Posted by: Xjlittle
bash, if..then, korn shell, ksh, Linux, unix, variable
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!