string comparison operators, what are they??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting string comparison operators, what are they??
# 1  
Old 02-13-2006
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  
Old 02-13-2006
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  
Old 02-13-2006
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  
Old 02-13-2006
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  
Old 02-13-2006
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  
Old 02-13-2006
Post the script you are using.

And the error message as-is.
# 7  
Old 02-13-2006
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk string comparison unterminated quoted string andrule of thumb

I have the logic below to look up for matches within the columns between the two files with awk. In the if statement is where the string comparison is attempted with == The issue seems to be with the operands, as 1. when " '${SECTOR}' " -- double quote followed by single quote -- awk matches... (1 Reply)
Discussion started by: deadyetagain
1 Replies

2. Shell Programming and Scripting

String comparison

hi team, i want to compare the below string from logs, but its is not working. if ]; then echo "restart some process" fi (4 Replies)
Discussion started by: mfaizan40
4 Replies

3. Homework & Coursework Questions

passing letters from an array into a string for string comparison

attempting the hangman program. This was an optional assignment from the professor. I have completed the logical coding, debugging now. ##I have an array $wordString that initializes to a string of dashes ##reflecting the number of letters in $theWord ##every time the user enters a (valid)... (5 Replies)
Discussion started by: lotsofideas
5 Replies

4. Shell Programming and Scripting

to extract string from main string and string comparison

continuing from my previous post, whose link is given below as a reference https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569 consider there is create table commands in a file for eg: CREATE TABLE `Blahblahblah` ( `id` int(11) NOT NULL... (2 Replies)
Discussion started by: vivek d r
2 Replies

5. Shell Programming and Scripting

BASH - set specific user variable via string operators

Apologies for the utter triviality of this question, but we all have to start somewhere! I've also tried searching but this question is pretty vague so I didn't (a) really know what to search for or (b) get many relevant hits to what I did search for. Anyway, I'm in the process of self-teaching... (1 Reply)
Discussion started by: u5j84
1 Replies

6. UNIX for Dummies Questions & Answers

Comparison operators: shortcut name

There are a number of comparison operators used in scripting and programming languages, such as the following: =, ==, ===, !=, =~, <, >, <=, >=, etc Is there a shortcut name for them, such as one has for as being any capital letter? If not, it would mean that I would have to list them all for a... (2 Replies)
Discussion started by: figaro
2 Replies

7. UNIX for Dummies Questions & Answers

string comparison

Hi Guys i need to write a script to check the file structure I have added the the file headers in the configuration file and execute the file at the start of the script. Now the function checkFileStructure() { echo "Inside the function" filetocheck=$1 fileheader=$2 if ] then... (1 Reply)
Discussion started by: Swapna173
1 Replies

8. Shell Programming and Scripting

string comparison

The script will read a bunch of names, and test if it contains "John", but as below apparently ~ does not work, so what is the easiest way to perform string comparison in bash shell script? thanks ... elif then echo "get John" .... (2 Replies)
Discussion started by: fedora
2 Replies

9. Shell Programming and Scripting

Get Comparison operators from with RexExp

Hello all im trying to get the Comparison operators from string with out much success I have : $myvar = "if (hhhh <= blah.count )" ; when I do : if ($myvar =~ m/.*().*/){ ....... } I keep getting the "=" and not "<=" why ? (3 Replies)
Discussion started by: umen
3 Replies

10. Programming

String Comparison

Hi all, I have a file like this ibhib=ere wefwfl=werfe sfdes=wef From this file, i need to get the lefthand side string with respect to the corresponding righthand side string. i.e, I need to get the string "ere" with respect to "ibhib". But i am stuck with how to compare a string... (1 Reply)
Discussion started by: abey
1 Replies
Login or Register to Ask a Question