tags:

views:

30

answers:

2

I have a question on the test command in the Korn shell (ksh). I know -ne is for comparing integers and != is for comparing strings. How will the test command behave if one argument is a string and the other is an integer. I have below conditions in my code and both are working properly.

myCount=1
myCount=`expr $myCount+ 0`
temp=`ps -aef | grep damn | wc -l`
if [ $temp -ne $myCount]; then
        echo ERROR Number
fi

if [ $temp != $myCount ]; then
        echo ERROR Strings
fi

Output is

ERROR Number
ERROR Strings
+1  A: 

The type is not relevant because it's a simple text substitution. In other words, the value of the variable $temp will be substituted in place of $temp (for example).

At least for the version of ksh I'm running, for the numeric comparison, if the value starts with a non-numeric, it will equate to 0. If it starts with a numeric but contains non-numerics, you will get an error.

For example:

$ export s1=xyz
$ export s2=7xyz
$ export i1=0
$ if [ $i1 -eq $s1 ]
> then
>     echo equal
> fi
equal
$ if [ $i1 -eq $s2 ]
> then
>     echo equal
> fi
ksh: 7xyz: bad number `7xyz'

However, based on your comments, that may not be the case for all versions of ksh.

Based on that, I would try to ensure that you use string comparisons for strings and numeric comparisons for numbers. Anything else may be non-portable.

paxdiablo
Did you mean, if [ $temp -eq $myCount ]; then echo ERROR Strings fi will always print the message as far as the value of temp is non numeric.
Sachin Chourasiya
Not quite, see the update. If the number _starts_ as a numeric but contains non-numerics, it will error. For a variable that doesn't start numeric, it's treated as 0.
paxdiablo
On My shell, $ export s1=xyz $ export s2=7xyz $ export i1=0 $ if [ $i1 -eq $s1 ] > then > echo equal > fi $ xyz: bad
Sachin Chourasiya
Interesting, that exact same code works on my version so I've updated the answer to basically "Don't do that" :-)
paxdiablo
It's not necessary to use `export` in this context.
Dennis Williamson
Yes, you're correct of course, but it's irrelevant to the question. I got into a habit early on in my career in exporting by default just so the variables are made available to subshells. Too much time spent wondering why my variables seemed to disappear, I guess :-)
paxdiablo
Will myCount=`expr $myCount+ 0` converts string value to a numeric value. It may sounds silly but its a question.
Sachin Chourasiya
Assuming that `$myCount` is numeric, I think that will work. But, if you want to force the type of a variable, look into `typeset`.
paxdiablo
A: 

Using ksh93 and GNU coreutils expr 7.4 your command:

myCount=`expr $myCount+ 0`

gives me a syntax error and sets myCount to null which causes both if statements to output "ksh: [: argument expected" errors. Try putting a space before the plus sign. Also, there needs to be a space before ].

You shouldn't need to convert myCount or temp to integers. The coercion of myCount using expr is completely unnecessary.

I prefer this form for comparing integers since it allows you to use symbolic comparison operators such as != and > instead of -ne and -gt:

if (( $temp != $myCount ))
Dennis Williamson