Browse Source

improved variable filling

main
deicidus 1 year ago
parent
commit
408533fd58
  1. 15
      spells/install-service-template

15
spells/install-service-template

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

Loading…
Cancel
Save