The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 02-13-2006
Registered User
 

Join Date: Feb 2006
Posts: 10
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
Reply With Quote
Forum Sponsor
  #2  
Old 02-13-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
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
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

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.
Reply With Quote
  #3  
Old 02-13-2006
Registered User
 

Join Date: Feb 2006
Posts: 10
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.
Reply With Quote
  #4  
Old 02-13-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
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.
Reply With Quote
  #5  
Old 02-13-2006
Registered User
 

Join Date: Feb 2006
Posts: 10
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.
Reply With Quote
  #6  
Old 02-13-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
Post the script you are using.

And the error message as-is.
Reply With Quote
  #7  
Old 02-13-2006
Registered User
 

Join Date: Feb 2006
Posts: 10
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 09:10 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0