- How To Write First UNIX Korn Shell Script Program
- Korn Shell Variables
- KSH IF Command Conditional Scripting Examples
How do I use if command under KSH to make decisions?
KSH offers program flow control using if conditional command. if command runs a set of command if some condition is true. For example, if directory /backup does not exists, create a new one so that your shell script can make backup to /backup directory.
KSH IF Syntax
if [ condition ] ; then // Condition satisfied and run this command else // Condition NOT satisfied and run this command fi
IF the variable $x is greater to 0, THEN print out a message. Otherwise (else), print out a different message. Following shell script to read 2 numbers and find the greaters of the two:
#!/bin/ksh x=5 y=10 if [ $x -ge $y ]; then echo "x = $x " else echo "y = $y" fi
Find out if file /etc/passwd exists or not:
#!/bin/ksh FILE=/etc/passwd DIR=/tmp/foo # ! means not # if file does exists if [ ! -f $FILE ]; then echo "Error $FILE does not exists!" else echo "$FILE found!" fi # if dir does not exists if [ ! -d $DIR ]; then echo "Error directory $DIR does not exists!" else echo "$DIR directory found!" fi
The -d option check for $DIR and make sure it is a directory. The -f option check for $FILE and make sure it is a regular file using conditional if command.
Featured Articles:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- My 10 UNIX Command Line Mistakes
- 10 Greatest Open Source Software Of 2009
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
- Email FAQ to a friend
- Download PDF version
- Printable version
- Comment RSS feed
- Last Updated: 07/2/09


{ 0 comments… add one now }