1823985 Members
4010 Online
109667 Solutions
New Discussion юеВ

syntax of if in Ksh

 
harikrishna
Advisor

syntax of if in Ksh

hi,
plz let me know the syntax of if in Ksh with or operator.is sytax for integer comparisions and string comparisions.
THANKS
5 REPLIES 5
G. Vrijhoeven
Honored Contributor

Re: syntax of if in Ksh

Hi,

Check man ksh:
if then else elif fi

Gideon
Mark Grant
Honored Contributor

Re: syntax of if in Ksh

Basic syntax

if expression
then
code
fi

comparisons are normally done with "test" which is also called "[" so

if [ $a > "abc" ]
then
echo "$a is alphabetically larger that abc"
fi

or for numerics

if [ $a -gt 10 ]

Alternatively, miss out the if altogether and rely on the && (and) and || (or) operators as in

[ $a -gt 10 ] && {
echo "$a is greater than ten"
}

Never preceed any demonstration with anything more predictive than "watch this"
Mark Greene_1
Honored Contributor

Re: syntax of if in Ksh

use the ksh built-in test syntax of [[ ]]

strings are compared vi > < and =

integers use -gt -lt -eq -ge -le

so you have tests like this:

if [[ "$String1" > "$String2" ]]; then

and

if [[ $Int1" -gt "$Int2" ]]; then

mark
the future will be a lot like now, only later
curt larson_1
Honored Contributor

Re: syntax of if in Ksh

for string comparisions use [[....]]

string == Pattern
true if string matches pattern Pattern.
Quote Pattern to treate it as a string.
= is obsolete and may be replaced in the future. Use == rather then =.

string != Pattern
string does not match pattern Pattern

string1 < string2
string1 comes before string2 in the collation order defined by the current locale

string1 > string2
string1 comes after string2 in the collation order defined by the current locale

for arithmetic expressions use ((....))

expression < expression
less then

expression > expression
greater then

expression <= expression
less then or equal to

expression >= expression
greater then or equal to

expression == expression
equal to

expression != expression
not equal to

the value of an expression with a comparison operator or a logical operator is 1 is non-zero (true), or 0 (false) otherwise.
Jim Butler
Valued Contributor

Re: syntax of if in Ksh