Press "Enter" to skip to content

Shell script Conditional

The need for conditionals in shell scripts have the ability to make life easier, be it from just a quick script or setting up environment variables for logins.

In the example below taken from a login script we needed to verify if a particular directory exists and if so set enviroment variables in a particular fashion to later be added to the $PATH, if not then set them another way. This in particular (though slightly sanitized) is part of a larger script used to determine what OS the server is running Solaris or HPUX, the particular function of the server can be termined if a certain directory exists.

[code]
if [ -d /apphome/$LOGNAME/serv/prcs/$LOGNAME ]
then
export APPCONFIG=/apphome/$LOGNAME/serv/prcs/$LOGNAME/APPCFG
else
export APPCONFIG=/apphome/$LOGNAME/serv/$LOGNAME/APPCFG
fi
[/code]

This list gives the file handling tests available.

-r True if file exists and is readable
-w True if file exists and is writable
-x True if file exists and is executable
-f True if file exists and is a regular file
-d True if file exists and is a directory
-c True if file exists and is a character special file
-b True if file exists and is a block special file
-p True if file exists and is a named pipe (FIFO)
-u True if file exists and is a SETUID file
-g True if file exists and is a SETGID file
-k True if file exists and the sticky bit is set
-s True if file exists and has a size greater than zero

From here this rest is up to your creativity how to employ these checks, using standardized scripts across all hosts just makes life easier.

——————————
The entire banking sector offers creditcard tends to hide certain facts from the customer. The credit cards cannot play a significant role in the matter of debt relief. The insurance quotes offering specific to policy holders are in the vogue these days. The real estate agents made sky rocketing profits in year 2004 to year 2006 all over the world with the sudden rise in properties and land.