But after running the script I end up with an empty file. Any suggestions?
a somewhat belated response... one possible solution is that your script could contain modifiable settings after the script's exit command.
to avoid accidental changes to anything but these settings, it would be a good idea to use an appropriate prefix like so:
@SETTINGS
@setting-homepage=http://www.goatse.cx
@setting-dns=4.4.2.2
below is an example that modifies two variables: one that specifies the default browser homepage (default: www.goatse.cx, new: www.google.com) and another to change DNS (default: 4.4.2.2, new: 8.8.8.8).
word of warning
sed is used to modify the contents of the line number containing the desired setting. if there are errors here, it will spaz out and overwrite every line in the script with sed-related madness. one solution could be to have the script write another script that then works to modify the original script. another way could be to include a conditional statement that only executes the sed substitution after confirming that the match exists. or perhaps more simply, matching the desired setting string instead of just the line number.
EDIT: it seems that attempting to perform string match with sed results in some strange script autophagy. i think you need to specify line numbers that are below the exit command.
#!/bin/bash
# find settings and extract their values
settingDefaultHomepage=$(grep ^@setting-homepage $0 | sed 's/@setting-homepage=*//')
settingDefaultDNS=$(grep ^@setting-dns $0 | sed 's/@setting-dns=*//')
echo "Default browser homepage = $settingDefaultHomepage"
echo "Default DNS = $settingDefaultDNS"
# find line number of setting, to be used when modifying the value
settingLineHomepage=$(awk '/^@setting-homepage/ {print FNR}' $0)
settingLineDNS=$(awk '/^@setting-dns/ {print FNR}' $0)
echo "Line of setting for browser homepage = $settingLineHomepage"
echo "Line of setting for DNS = $settingLineDNS"
# modify the settings using line number above
sed -i $settingLineHomepage's/.*/@setting-homepage=www.google.com/' $0
sed -i $settingLineDNS's/.*/@setting-dns=8.8.8.8/' $0
exit
@SETTINGS
@setting-homepage=www.goatse.cx
@setting-dns=4.4.2.2