string comparison operators, what are they??

hi guys

im a newbie to shell scripting and would appreciate any help possible. my questions will be very straight forward and relatively trivial to most of you.

as the thread title is asking, what are the operators??

i know there's the != and =, but for bourne and c shell, what are the lexical comparison operators?

i read from a website that you could use the < and > as long as you put the forward slash in front of them so that the shell interpreter can identify it as a string operator as opposed to a redirect operator.

this is a snippet of code i've been trying the > and < operators but the tcshell wont work with it. i'musing tcshell while i want to know for bourne and c shell because that's the only shell i have access to at the moment.

anyways here's the code

######################################
set VAR1="hi"
set VAR2="bye"

if [ $VAR1 \< $VAR2 ] ; then
echo "$VAR1 is lexically less than $VAR2"
else
echo "$VAR1 is lexically greater than $VAR2"
fi
exit 0

######################################

basically the shell can't interpret the \< part. any help appreciated.

thanks in advance

set VAR1="hi"
set VAR2="bye"

if [ $VAR1 \< $VAR2 ] ; then
echo "$VAR1 is lexically less than $VAR2"
else
echo "$VAR1 is lexically greater than $VAR2"
fi
exit 0

In sh/ksh, you dont need to escape the < or >. They are recognized operators. So the hi-lited line would become if [ $VAR1 < $VAR2 ] ; then

Now in the unix world, < or > stands for redirecting input, output respectively. Since you need the literal meaning of "lesser than", you should use the following construct. Again the hi-lited line would become

if [[ $VAR1 < $VAR2 ]] ; then

Notice the extra [].

From man ksh

       [[ expression ]]
              Similar to the test and [ ... ] commands (described later), with
              the following exceptions:
                �    There  are two additional binary operators: < and > which
                     return true if their first string operand is  less  than,
                     or  greater  than,  their  second string operand, respec-
                     tively.

thanks for the heads, up, i will have to try that out tomorrow, it apparently doesnt work on my school's intranet.

it says it cant interpret the double brackets "[["

is sh and ksh similar to bash?

my school's vmware workstation has the bash shell so i'm going to try the < and > operators for strings there.

You are using tcsh. Make the first line of your script look like

#! /bin/sh

or

#! /bin/ksh

Now that you mention VMWare, I dont have access to a VMWare machine right now. But try including that line as the first line in your script and then run.

yeah i already had the #!/bin/sh before the script code and it wont work

it says it cannot open bye (second variable evaluates to bye and it thinks of bye as a file meaning it interprets the "<" as a redirect operator).

and i also tried not using that first line on the script and using double brackets and it says: [[ not found.

Post the script you are using.

And the error message as-is.


#! /bin/sh

read A B

if [ $A < $B ] ; then
        echo "$A is less than $B"
else
        echo "$A is greater than $B"
fi

exit 0

intranet (119) % sh strcmp.sh
hi bye
strcmp.sh: bye: cannot open <-- error message

so i'm using the < as a string operator but it detects it as a redirect.

and basically A and B are read in from the keyboard

thanks for your patience by the way, i know it's late, at least where i live hehe

#! /bin/sh

read A B

if [[ $A < $B ]] ; then
        echo "$A is less than $B"
else
        echo "$A is greater than $B"
fi

exit 0

i tried the other one too with the double brackets and it doesnt give me the right result. it does show an error but will go through the whole execution of the program, but as mentioned before, the result isn't correct

this is what happens

intranet (121) % sh strcmp.sh
hi bye
strcmp.sh: [[: not found
hi is greater than bye

intranet (122) % sh strcmp.sh
bye hi
strcmp.sh: [[: not found
bye is greater than hi

so in the first run, i type in hi and bye, it says the [[ part of the code not found...i take it that it cant interpret double brackets.

and it goes to the else statement.

in the second run i type in bye before hi, and it goes to the else statement again, while it should've been the first if statement.

i'm going to have to try it out on a real c shell to see what the result turns out to be.

I guess it must have something to do with the vmware machine.

i'mnot using it with vmware at the moment, but i'm using SPARC, i think it's based on unix, hence why it keeps interpreting the > and < as redirect. however the double brackets arent working either. i found another forum where a user had posted his code which looks just like the one with the double brackets and he's having the same problem.

i will have to test both codes tomorrow on my school's vmware.

hi, vino, i just wanted to say thanks for your help. i d/led cygwin and ran the code for the double brackets and it works like a charm. i think cygwin is a c shell emulator? or perhaps bourne shell. either way it's working great.

I realize that this is too late to help Balzarus, but it might help someone else. The error you were getting, namely :

is caused because you did not have the required whitespace around your brackets. The double brackets tell ksh to use it's own, internal evaluation criteria.

For example:

if [[$A < $B]]

will not work

However:

if [[ $A < $B ]]

will work