Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

want to run command from variable

Status
Not open for further replies.

cp2000

Technical User
Dec 31, 2002
81
US
I'm running a bash shell. version 2.05b I would like to be able to load a commandline into an environmnetal variable and then runn the command using that variable.

It seems to work OK until I get special characters in the commandline. For instance
Code:
REMOVECOMMAND="rm -f $DESTINATIONDIR/$SOURCEHOSTNAME/$TARFILENAME{,.gz}"
$REMOVECOMMAND
runs just fine!! But
Code:
BACKUPCOMMAND="tar -cf $DESTINATIONDIR/$SOURCEHOSTNAME $TARFILENAME $SOURCEDIRECTORY > $DESTINATIONDIR/$SOURCEHOSTNAME/$LISTNAME 2>>$MAILFILE"
$BACKUPCOMMAND
fails!
Looking at the results of "set -x" shows that the redirects are being put in single quotes. How do I escape the redirects?
 
In the man (or info) page, take a look at the eval shell builtin.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
tried it...it runs but it doesn't solve the problem
Code:
eval $BACKUPCOMMAND
 
I looked at doing something like this in Korn shell and ended up using aliases, eg:

command="tar -cf $DESTINATIONDIR/$SOURCEHOSTNAME $TARFILENAME $SOURCEDIRECTORY > $DESTINATIONDIR/$SOURCEHOSTNAME/$LISTNAME 2>>$MAILFILE"
alias BACKUPCOMMAND=${command}

In my case I was extracting information from a database to generate the command variable (using grep & sed) which is why I used a couple of lines of scripting. I expect it can be done in one.

Just an idea, I hope it helps.

Mike

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top