string comparison operators, what are they??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting string comparison operators, what are they??
# 8  
Old 02-13-2006
Quote:
Originally Posted by ballazrus
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
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

# 9  
Old 02-13-2006
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.
# 10  
Old 02-13-2006
I guess it must have something to do with the vmware machine.
# 11  
Old 02-13-2006
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.
# 12  
Old 02-13-2006
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.
# 13  
Old 04-07-2009
I realize that this is too late to help Balzarus, but it might help someone else. The error you were getting, namely :

Quote:
strcmp.sh: [[: not found
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.

For example:

if [[$A < $B]]

will not work

However:

if [[ $A < $B ]]

will work
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