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 and shell scripting languages 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 05:09 AM
string comparison Jatsui Shell Programming and Scripting 5 02-04-2008 04:28 PM
string comparison fedora Shell Programming and Scripting 2 01-03-2007 03:20 PM
Get Comparison operators from with RexExp umen Shell Programming and Scripting 3 07-19-2006 11:38 AM
String Comparison abey High Level Programming 1 10-19-2005 01:08 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-13-2006
ballazrus ballazrus is offline
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
  #2 (permalink)  
Old 02-13-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
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.
  #3 (permalink)  
Old 02-13-2006
ballazrus ballazrus is offline
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.
  #4 (permalink)  
Old 02-13-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
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 (permalink)  
Old 02-13-2006
ballazrus ballazrus is offline
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.
  #6 (permalink)  
Old 02-13-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

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

And the error message as-is.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:32 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0