![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [sh] String comparison operators | userix | Shell Programming and Scripting | 1 | 05-16-2008 01:09 AM |
| string comparison | Jatsui | Shell Programming and Scripting | 5 | 02-04-2008 01:28 PM |
| string comparison | fedora | Shell Programming and Scripting | 2 | 01-03-2007 12:20 PM |
| Get Comparison operators from with RexExp | umen | Shell Programming and Scripting | 3 | 07-19-2006 07:38 AM |
| String Comparison | abey | High Level Programming | 1 | 10-19-2005 09:08 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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 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 Code:
[[ 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.
|
|
#3
|
|||
|
|||
|
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. |
|
#4
|
||||
|
||||
|
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. |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
||||
|
||||
|
Post the script you are using.
And the error message as-is. |
|
#7
|
|||
|
|||
|
Code:
#! /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 |
|||
| Google The UNIX and Linux Forums |