Bash shell script tip: Run commands from a variable

  • : Function ereg() is deprecated in /home/bulahema/public_html/old/includes/file.inc on line 645.
  • : Function ereg() is deprecated in /home/bulahema/public_html/old/includes/file.inc on line 645.
  • : Function ereg() is deprecated in /home/bulahema/public_html/old/includes/file.inc on line 645.
  • : Function ereg() is deprecated in /home/bulahema/public_html/old/includes/file.inc on line 645.
  • : Function ereg() is deprecated in /home/bulahema/public_html/old/includes/file.inc on line 645.
  • : Function ereg() is deprecated in /home/bulahema/public_html/old/includes/file.inc on line 645.
  • : Function ereg() is deprecated in /home/bulahema/public_html/old/includes/file.inc on line 645.
  • : Function ereg() is deprecated in /home/bulahema/public_html/old/includes/file.inc on line 645.
  • : Function ereg() is deprecated in /home/bulahema/public_html/old/includes/file.inc on line 645.
  • : Function ereg() is deprecated in /home/bulahema/public_html/old/includes/file.inc on line 645.
  • : Function ereg() is deprecated in /home/bulahema/public_html/old/includes/file.inc on line 645.
  • : Function ereg() is deprecated in /home/bulahema/public_html/old/includes/file.inc on line 645.
  • : Function ereg() is deprecated in /home/bulahema/public_html/old/includes/file.inc on line 645.
  • : Function ereg() is deprecated in /home/bulahema/public_html/old/includes/file.inc on line 645.
  • : Function ereg() is deprecated in /home/bulahema/public_html/old/includes/file.inc on line 645.
  • : Function ereg() is deprecated in /home/bulahema/public_html/old/includes/file.inc on line 645.
  • : Function ereg() is deprecated in /home/bulahema/public_html/old/includes/file.inc on line 645.
  • : Function ereg() is deprecated in /home/bulahema/public_html/old/includes/file.inc on line 645.
  • : Function ereg() is deprecated in /home/bulahema/public_html/old/includes/file.inc on line 645.
  • : Function ereg() is deprecated in /home/bulahema/public_html/old/includes/file.inc on line 645.
  • : Function ereg() is deprecated in /home/bulahema/public_html/old/includes/file.inc on line 645.
  • : Function ereg() is deprecated in /home/bulahema/public_html/old/includes/file.inc on line 645.
  • : Function ereg() is deprecated in /home/bulahema/public_html/old/includes/file.inc on line 645.
  • : Function ereg() is deprecated in /home/bulahema/public_html/old/includes/file.inc on line 645.
  • : Function ereg() is deprecated in /home/bulahema/public_html/old/includes/file.inc on line 645.
  • : Function ereg() is deprecated in /home/bulahema/public_html/old/includes/file.inc on line 645.
  • : Function ereg() is deprecated in /home/bulahema/public_html/old/includes/file.inc on line 645.
  • : Function ereg() is deprecated in /home/bulahema/public_html/old/includes/file.inc on line 645.

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)...

Posted in Tips, UNIX, Howto, Shell scripting, Linux | Print | Top Of Page | Last updated on: April 22, 2007

2 Responses to “Bash shell script tip: Run commands from a variable”

  1. Bruno Says:

    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

  2. Mahesh Says:

    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