sed problems when run in $( ... )
Hi all,
I'm trying to write a script that will setup a new website on my development box - one that automatically creates its Subversion repository, checks out a local working copy, creates folders for the apache scripts and log files, and updates the apache config. It's all going swimmingly, except for the last bit - automatically updating the apache config. The "template" apache config for each new website is stored in a file with dummy entries where the real values should go (so for instance WEBSITE_NAME will be replaced by a display name for the website), and I'm trying to use sed to replace these values in my script. Because of what the script is doing, I've added a "debug" mode that echo's the commands to be run, rather than actually running them (so I don't mess up my system), so the commands are run from a variable (for instance $( $cmd )). This bit seems to work fine, except when I try and use sed to update the config file, in which case I get the following error that I haven't been able to resolve after several hours of Googling: sed: -e expression #1, char 1: unknown command: `'' I've managed to whittle the error down to the following: Code:
# Create the sed command I'm running an up-to-date Fedora 11, with sed version 4.1.5 and bash 4.0.16(1)-release. Thanks! PS. For the record, /svn/svn-admin/httpd-conf-skel contains Code:
# WEBSITE_NAME |
The problem is the way the shell quotes the string in $cmd. If you use bash -x you can see it:
Code:
$ cat test.sh Code:
$ cat test.sh |
Quote:
The reason I'm not running the command directly in a $( ... ) is that there's multiple commands in the script, each of which is run through a common function that checks to see if the debug mode has been set - if it has, it simply echo's the command to STDOUT, otherwise its tries running it. The "fuller" command that is causing a problem in my script is this: Code:
# Display command if debugging, otherise run Code:
# State debug mode Thanks again. |
Use eval to reverse the bash quoting, that is let bash assign the command using quotes and other stuff, then use eval to evaluate what the bash has written in the cmd string. You should not have any problem anymore.
Code:
output="$(eval $cmd)" |
Works perfectly, thanks for your help!
|
All times are GMT -5. The time now is 06:29 AM. |