1833124 Members
3548 Online
110051 Solutions
New Discussion

kornshell

 
mango_1
Frequent Advisor

kornshell

hello all! I'd like to ask all the ksh gurus. if its possible to assign an expression/condition (similar to a boolean) to a variable example.

example:
test1=[[ 0 < 1 ]]
and
test1 will be assigned the value of 0 since this expression evaluates to true. Can I use eval? I tried but to no avail.

I also have another question:
I have a string test2="ab"

example:
if [[ $test2? = "abc" ]] ; then
echo "equal"
fi

which should print equal. I was trying this but it wouldn't matched.

thanks so much guys!
5 REPLIES 5
John Dvorchak
Honored Contributor

Re: kornshell

example:
if [[ $test2? = "abc" ]] ; then
echo "equal"
fi


Should have double quotes around the var also and I believe single square brackets:

example:
if [ "$test2" = "abc" ] ; then
echo "equal"
fi
If it has wheels or a skirt, you can't afford it.
Hai Nguyen_1
Honored Contributor

Re: kornshell

example:
if [[ $test2? = "abc" ]] ; then
echo "equal"
fi

should be:

if [[ ${test2}c = "abc" ]] ; then
echo "equal"
fi
curt larson_1
Honored Contributor

Re: kornshell

ksh doesn't not support boolean operators.

versions newer then 88, ie newer then what is one hp-ux, has a builtin true command. it does nothing and always has an exit code of 0. similiarly with false except it has an exit code of 1.

the version on hp-ux usually implements these as an alias. i've used the /usr/bin/true and false commands at times. as an example.

notDone=/usr/bin/true
a=a
# this will stay in the while loop until
# the letter b is input
#
while $notDone
do
if [ $a = b] ;then
notDone=/usr/bin/false #drops out of while loop
else
print "type in a letter"
read answer
fi
done
Jdamian
Respected Contributor

Re: kornshell

You can use the following:

[[ 2 -gt 0 ]]
A=$?

You can use double brackets to evaluate arithmetic operations:

A=$(( 2>0 ))

Remember that:

a) arithmetic symbols as '>', '>=', '<', '<=' and bitwise operators (&,|,>>,<<) are only available using double brackets

echo $(( 1 << 4 )) # prints 16

b) relational operators && and || (instead of -a and -o used in test) are available using both double square brackets and double brackets

Jdamian
Respected Contributor

Re: kornshell

an important remark:

when (( ... )) evaluates a conditional expression (for instance, (( 2>1 )) ) computes

1 when it is TRUE
0 when it is FALSE

BUT the return value is

0 when it is TRUE
1 when it is FALSE

Run the following examples:

$ (( 2<3 )) && echo true || echo false # prints true
$ echo $(( 2<3 )) # prints 1