if condition then command1 if condition is true or if exit status of condition is 0 (zero) ... ... fi
#!/bin/sh # #Script to print file # if cat $1 then echo -e "\n\nFile $1, found and successfully echoed" fi
test expression OR [ expression ]
#!/bin/sh # # Script to see whether argument is positive # if test $1 -gt 0 then echo "$1 number is positive" fi
Mathematical | Operator in Shell |
-eq | is equal to ; if test 5 -eq 6 ; if [ 5 -eq 6 ] |
-ne | is not equal to ; if test 5 -ne 6; if [ 5 -ne 6 ] |
-lt | is less than ; if test 5 -lt 6; if [ 5 -lt 6 ] |
-le | is less than or equal to ; if test 5 -le 6; if [ 5 -le 6 ] |
-gt | is greater than ; if test 5 -gt 6; if [ 5 -gt 6 ] |
-ge | is greater than or equal to ; if test 5 -ge 6; if [ 5 -ge 6 ] |
String Comparisons | Operator Meaning |
string1 = string2 | string1 is equal to string2 |
string1 != string2 | string1 is NOT equal to string2 |
string1 | string1 is NOT NULL or not defined |
-n string1 | string1 is NOT NULL and does exist |
-z string1 | string1 is NULL and does exist |
File and directory | Test Meaning |
-s file | Non empty file |
-f file | Is File exist or normal file and not a directory |
-d dir | Is Directory exist and not a file |
-w file | Is writeable file |
-r file | Is read-only file |
-x file | Is file is executable |
Logical Operators | Operator Meaning |
! expression | Logical NOT |
expression1 -a expression2 | Logical AND |
expression1 -o expression2 | Logical OR |
if condition then condition is zero (true - 0) execute all commands up to else statement else if condition is not true then execute all commands up to fi fi
#!/bin/sh # # Script to see whether argument is positive or negative # if [ $# -eq 0 ] then echo "$0 : You must give/supply one integers" exit 1 fi if test $1 -gt 0 then echo "$1 number is positive" else echo "$1 number is negative" fi
if condition then if condition then do this else do this fi else do this fihttp://siber.cankaya.edu.tr/SystemsProgramming/cfiles/nestedifnestedif
#!/bin/sh # # Script to see nesting of ifs # osch=0 echo "1. Unix (Sun Os)" echo "2. Linux (Red Hat)" echo -n "Select your os choice [1 or 2]? " read osch if [ $osch -eq 1 ] ; then echo "You Pick up Unix (Sun Os)" else # nested if i.e. if within if # if [ $osch -eq 2 ] ; then echo "You Pick up Linux (Red Hat)" else echo "What you don't like Unix/Linux OS." fi fi
if condition then condition is zero (true - 0) execute all commands up to elif statement elif condition1 then condition1 is zero (true - 0) execute all commands up to elif statement elif condition2 then condition2 is zero (true - 0) execute all commands up to elif statement else None of the above condition,condition1,condition2 are true (i.e. all of the above nonzero or false) execute all commands up to fi fiFor multilevel if-then-else statement try the following script: http://siber.cankaya.edu.tr/SystemsProgramming/cfiles/elfelf
#!/bin/sh # Script to test if..elif...else # if [ $1 -gt 0 ]; then echo "$1 is positive" elif [ $1 -lt 0 ] then echo "$1 is negative" elif [ $1 -eq 0 ] then echo "$1 is zero" else echo "Opps! $1 is not number, give number" fiInteger comparison is expected.