Open Source Software and Linux

Jan 24 2009   8:01PM GMT

Setting script variables using the if..then statement

John Little Profile: Xjlittle

Recently I found myself wanting to set some variables in a script that I was writing using the if..then statement. I am using the Korn shell but this is bash compatible.

I wanted to do this as the variable command changed depending on if the script was running on Linux, HP-UX or AIX. This saved a ton of typing the same test over and over throughout the script which is about 600 lines.

The basic form of the script is

if [[ $STRING != string ]]; then

As you can see the if statement is built around the VARIABLE rather than VARIABLE=if statement. Doing it in that form either won’t return any output or give you output that is not useful.

A real world example

if [ $VENDOR != "Linux" ];then
BACKUPHEADER=log "$(date '+%D %T') Checking header."
BACKUPHEADER=/bin/logger -t "VALIDATE LINUX BACKUP: $(date '+%D %T') Checking header."

As you can see from the above the Unix command for sending output to syslog is log and is logger on Linux. Now imagine having to run various tests throughout the script and sending the output to syslog depending on the OS. As I mentioned, it saved quite a bit of typing.

Hope this helps you somewhere!


 Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: