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.
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop














{ 0 comments… add one now }