Browse Source

improved variable filling

main
deicidus 2 years ago
parent
commit
408533fd58
  1. 19
      spells/install-service-template

19
spells/install-service-template

@ -34,20 +34,27 @@ for arg in "$@"; do
substitution_cmd="${substitution_cmd}s|\\\$${arg%%=*}|${arg#*=}|g;" substitution_cmd="${substitution_cmd}s|\\\$${arg%%=*}|${arg#*=}|g;"
done done
# Perform substitutions # Perform substitutions into a temporary file in /tmp
sudo sh -c "sed \"$substitution_cmd\" \"$template_path\" > \"$service_path\"" tmp_file="/tmp/tmp_service"
sed "$substitution_cmd" "$template_path" > "$tmp_file"
# Ask for missing variables # Ask for missing variables
while IFS= read -r line; do while IFS= read -r line; do
if echo "$line" | grep -q '\$'; then if echo "$line" | grep -q '\$'; then
var=$(echo "$line" | sed -n -e 's/^.*\$\([A-Z_]*\).*$/\1/p') var=$(echo "$line" | sed -n -e 's/^.*\$\([A-Z_]*\).*$/\1/p')
if [ -n "$var" ]; then if [ -n "$var" ]; then
echo "Enter a value for $var: " value="${!var}"
read -r value if [ -z "$value" ]; then
sed -i "s|\$$var|$value|g" "$service_path" echo "Enter a value for $var: "
read -r value
fi
sed -i "s|\$$var|$value|g" "$tmp_file"
fi fi
fi fi
done < "$service_path" done < "$tmp_file"
# Move the temporary file to the final destination
sudo mv "$tmp_file" "$service_path"
# Set permissions # Set permissions
sudo chmod 644 "$service_path" sudo chmod 644 "$service_path"

Loading…
Cancel
Save