Bash shell script tip: Run commands from a variable
There are many situations in which you may want to run different command in your shell script depending on requirements and circumstances. So how do you select and run a command?
The old way...
The old way is use the case statement or if..else..fi command. For example:
#!/bin/bash if [ this -eq that ];then command1 else command2 fi
The new way...
BASH allows you to assign or store a command name in a variable. For example create a variable called CMD:
#!/bin/bash [ this -eq that ] && CMD=”/bin/ls” || CMD="/bin/date" $CMD
Above shell script is a simple example of this concept. CMD variable stores /bin/ls or /bin/date command. To run command you simply type a variable name. So you are using contents of a variable to run command itself depending on circumstances. For example write a backup script to backup data to tape, NAS/ftp server or using scp command. You can run a script as follows:
/path/to/backup.sh nas
/path/to/backup.sh tape
/path/to/backup.sh scp
Now depend upon circumstances you need to swap a command for backup task. Here is a sample script:
#!/bin/bash CMD="" DIRS="/etc /home /www /data1 /data2 /var/log /var/spool/mail" FILE="/backup/$(hostname)-$(date +'%m-%d-%y').tar.gz" [ "$1" == "nas" ] && CMD="lftp -u user,password -e 'cd /dump/; mput /backup/*; quit' nas.mylan.com" || : [ "$1" == "scp" ] && CMD="scp /backup/* scponly@dumpserver:incoming' username" || : [ "$1" == "tape" ] && CMD='tar -cf /dev/st0 /backup/*' || : [ "$CMD" == "" ] && exit 1 || : # make a backup tar -zcvf $FILE $DIRS # Now depend upon circumstances run a backup command $CMD
Now you can run a different backup command in your script depending on parameter passed to script itself. You can also use this trick to write a cross platform shell script.
E-mail this to a friend
Printable version
You may also be interested in other helpful articles:
- BASH shell scripting tip: Set default values for variable
- Executing script or command on the last day of a month
- Linux / UNIX: display time of different time zones using TZ environment variable
- Take action or execute a command based upon shell script name
- nixCraft FAQ roundup - Jan / 15 / 2007
Discussion on This Article:
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!



It would be nice to write just 2 lines about the way it works : (condition-A && condition-B) : bash evaluate condition-B only if condition-A is true. If condition-A is false, the result is FALSE anyway and bash doesn’t need to evaluate condition-B. Then it goes thru all the || conditions.
Or point to a page that explain this.
Anyway, it is very nice to be able to read these tips on a regular basis.
Thanks
Hi
I want to take tar-backup of user home folders on RH9 Linux.
My short script is as follows :
#!/bin/bash
#pwd
cd /home
tar cvzf /backup/archive/usr1-`date +%C%y%m%d`.tar.gz usr1
tar cvzf /backup/archive/usr2-`date +%C%y%m%d`.tar.gz usr2
.
.
So On !!!!!!!!!
If i am running this script from cmd , it works fine. But If i cron the job it stops aborted on 2nd directory itself with half-completed tar file.
How to solve this problem? I am not so novice in Linux. Pls help.
Regards /Mahesh
can you guys please send me linux shell script commands for backup documents home folder, i need it
very badly because i need to submit it on 24/9/07 to my teacher..please any one kindly send me this..
my e-mail address raihan_alam@hotmail.com
I experienced another problem.
If I call script files, input methods (read ) are ignored.
find /media/private -iname "*.bash" | while read file
do
echo "Executing: $file"
$file -m
if [ "$?" -ne 0 ]
then
echo “Error executing $file”
exit 1
fi
echo “done”
This is not just a bash feature as you imply. $CMD works with Korn shell and most other shells.