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.
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.
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.
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'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.