| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 |
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.
If you get stuck on something or have a question, post your question to our excellent Linux tech support forum. You can subscribe to our free e-mail newsletter or RSS feed or Leave a reply/comment ( 2 ).
You may also be interested in (skip to comment)...
- BASH shell scripting tip: Set default values for variable
- Executing script or command on the last day of a month
- Take action or execute a command based upon shell script name
- Shell scripting (BASH) : How to create temporary random file name
- nixCraft FAQ roundup

April 24th, 2007 at 7:41 am
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
May 4th, 2007 (5 weeks ago) at 6:54 am
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