Need help with script writing: Storing cmd in variable, print variable, then exe cmds
I have a script which contains sections that store a command to a variable, prints out the command, then executes it.
Example: Code:
MYCOMMAND="cp file1.txt $NEWFILE" The above works when I save a single command to the string like I did above. However I want to store a string made up of multiple commands separated by && or ;. Example: Code:
MYCOMMAND="cp file1.txt $NEWFILE && rm file1.txt && echo done" How I can save a string with multiple commands in it to a single variable, echo that variable so the commands that are going to be run are printed out first, and then actually run the commands stored in that variable? |
NEWFILE=emp.2.txt
MYCOMMAND="cp file1.txt $NEWFILE ; rm file1.txt ; echo done" echo "Running $MYCOMMAND" echo $MYCOMMAND |sh |
You can use 'set -x'
Either this way if you want to trace every command in your script: Code:
#!/bin/bash -x Code:
#!/bin/bash |
Quote:
But, if any of the command arguments include whitespace (space, tab or newline), it will fail. More information about wanting to execute complex commands held in a bash variable in Greg's Bash FAQ. |
All times are GMT -5. The time now is 10:14 AM. |