While-read and if-elif


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting While-read and if-elif
# 8  
Old 09-13-2012
Thanks guys! It's working well now! Thanks again.
# 9  
Old 09-13-2012
bash man page:
Quote:
CONDITIONAL EXPRESSIONS
Conditional expressions are used by the [[ compound command and the test and [ builtin commands to test file attributes and perform string and arithmetic comparisons.
...
string1 > string2
True if string1 sorts after string2 lexicographically.
arg1 OP arg2
OP is one of -eq, -ne, -lt, -le, -gt, or -ge. These arithmetic binary operators return true if arg1 is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to arg2, respectively. Arg1 and arg2 may be positive or negative integers.
# 10  
Old 09-13-2012
Quote:
Originally Posted by pamu
Hi Rudic,

I am not able to understand how we can use < and > signs for string comparison....????
According to the standards, you can't with test... and [ ... ],
but in [[ ... ]], < and > are string comparison operators. The -lt and -gt operators perform numeric comparisons in all three forms of these testing methods.

In bash, the < and > are string comparisons in test ... and [ ... ]. In ksh, the < and > will be treated as redirection operators, not string comparisons. I.e., in ksh:
Code:
if [ 100 > 200 ]
then echo gt
else echo not gt
fi

will print gt because 100 is not an empty string and will create a file named 200 because > is a redirection operator. But in bash, this same code will print not gt and will not create a file. Both of these behaviors conform to the standards because the standards don't specify any meaning for < and > in these two cases.

=================
I take part of this back. Despite what the bash man page says, at least on Mac OS X, bash and ksh both treat < and > as redirection operators in test and [.

Last edited by Don Cragun; 09-13-2012 at 06:40 AM.. Reason: Update based on testing bash on OS X Lion
This User Gave Thanks to Don Cragun For This Post:
# 11  
Old 09-13-2012
Thanks Don Cragun and Rudic,

I have tried out few things. just see below..

For String Operation -
Code:
$ if [[ "b" -gt "a" ]]; then  echo "gt"; else  echo "not gt"; fi
not gt

$ if [[ "b" > "a" ]]; then  echo "gt"; else  echo "not gt"; fi
gt

$ if [[ "a" -gt "b" ]]; then  echo "gt"; else  echo "not gt"; fi
not gt

$ if [[ "a" > "b" ]]; then  echo "gt"; else  echo "not gt"; fi
not gt

$ if [[ a -gt b ]]; then  echo "gt"; else  echo "not gt"; fi
not gt

$ if [[ a > b ]]; then  echo "gt"; else  echo "not gt"; fi
not gt

$ if [[ b -gt a ]]; then  echo "gt"; else  echo "not gt"; fi
not gt

$ if [[ b > a ]]; then  echo "gt"; else  echo "not gt"; fi
gt

For numeric operation -
Code:
$ if [[ "1" -gt "2" ]]; then  echo "gt"; else  echo "not gt"; fi
not gt

$ if [[ "1" > "2" ]]; then  echo "gt"; else  echo "not gt"; fi
not gt

$ if [[ "2" -gt "1" ]]; then  echo "gt"; else  echo "not gt"; fi
gt

$ if [[ "2" > "1" ]]; then  echo "gt"; else  echo "not gt"; fi
gt

$ if [[ 1 -gt 2 ]]; then  echo "gt"; else  echo "not gt"; fi
not gt

$ if [[ 1 > 2 ]]; then  echo "gt"; else  echo "not gt"; fi
not gt

$ if [[ 2 -gt 1 ]]; then  echo "gt"; else  echo "not gt"; fi
gt

$ if [[ 2 > 1 ]]; then  echo "gt"; else  echo "not gt"; fi
gt

From this i came to conclusion that -

1) Never use -gt and -ltfor string comparison.
2) Use > and < for string comparison and -gt and -lt for numeric comparison..
3) String and numbers are compared in sorted order as
Number - 1 - 9 ( like 9 is greater than 1)
String - A,B,C...Z, a,b,c,....z ( like z is greater than a)

these are my few. you can add if you feel so.....Smilie

Last edited by pamu; 09-13-2012 at 07:33 AM.. Reason: added info
# 12  
Old 09-13-2012
@pamu: I think this would be a more evident example:
Code:
$ [[ 21 > 3 ]] && echo gt || echo lt
lt
$ [[ 21 -gt 3 ]] && echo gt || echo lt
gt

21 interpreted as a string compares less than 3, while as an integer, it compares greater. This is valid for bash; maybe someone can comment on other shells.

Last edited by RudiC; 09-13-2012 at 07:33 AM..
This User Gave Thanks to RudiC For This Post:
# 13  
Old 09-13-2012
Quote:
Originally Posted by RudiC
@pamu: I think this would be the most striking example:
Code:
$ [[ 21 > 3 ]] && echo gt || echo lt
lt
$ [[ 21 -gt 3 ]] && echo gt || echo lt
gt

21 interpreted as a string compares less than 3, while as an integer, it compares greater. This is valid for bash; maybe someone can comment on other shells.
That's good one.. it clears useless use of < and > while numeric comparison.. removed from numeric comparison...Smilie

As explained in my previous post < and > signs treats as string so they compare each digit by digit. such as..

Code:
$ [[ 211 > 12 ]] && echo gt || echo lt        # Here it compares 2 with 1 so 2 is greater than 1 as per sort pattern so gt.
gt

$ [[ 211 > 32 ]] && echo gt || echo lt         #Here it compares 2 with 3 so 3 is greater than 2 as per sort pattern so lt.
lt

Thanks Rudic...

Last edited by pamu; 09-13-2012 at 07:45 AM..
# 14  
Old 09-13-2012
A mnemonic: For numeric comparison, use alphabetic operators (eq, ne, gt, ge, lt, le). Makes perfect sense! Smilie

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with elif and else

Hello experts, I am having problems with ELIF. Please see the below code : read a; read b; read c; if || || ; then echo "EQUILATERAL" elif || || ; then echo "SCALENE" else echo "ISOSCELES" fi This code works fine for the EQUILATERAL case but fails completely for the... (5 Replies)
Discussion started by: H squared
5 Replies

2. Shell Programming and Scripting

If / elif

Hello all, I have a scenario where I take user input values and accordingly take actions say I run a script with sh scriptname -x GB -e txt (txt can also be text) I have used if clause for the first input (-x GB)and it is working fine Now for second the scenario is if then echo... (3 Replies)
Discussion started by: nnani
3 Replies

3. UNIX for Advanced & Expert Users

if else elif

Hi all,i have configured the following script to check if the file exists or not, #!/bin/sh sleep 30 { FILEFULL=$1`date +$2`* if ; then echo $FILEFULL exist else echo "$FILEFULL File not Found" | mail -s 'server' myaccount@mydomain.com fi } but i have a problem, i need to... (2 Replies)
Discussion started by: charli1
2 Replies

4. Homework & Coursework Questions

if/elif help

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: This is my problem for the class. Write a script that asks for the user's age. If it is equal to or higher... (6 Replies)
Discussion started by: aggie6970
6 Replies

5. Shell Programming and Scripting

If, elif, else statements help

I am in need of some help. I am not an advance scripter or programmer, I have novice knowledge. I cannot get a script to work as expected. The script basically does an FTP (which I have work perfectly) and writes to a log. However, I want the script to echo "Not Connected" if the FTP fails to... (2 Replies)
Discussion started by: anthonyon
2 Replies

6. Shell Programming and Scripting

If -elif-else error.

I am getting below error from this code (which is at line 24): if ] #this is line24 in code then mv $File_source_path/$File_name $File_name'_'`date '+%d%m%y'` Error: line 24: Any help with the syntax. I am putting 2 condition with 'AND' clause. This is bash shell. (2 Replies)
Discussion started by: amit.mathur08
2 Replies

7. Shell Programming and Scripting

Query on If-Elif!!

Script Gurus, Need your help in getting this script to come out of logical error : I have pasted the script below: This script finds disk utilzation ... the script is written for both AIX and SUN OS... and option will be given in the initial to select DB or Non DB... its required for my prj...... (1 Reply)
Discussion started by: vangalli
1 Replies

8. UNIX for Dummies Questions & Answers

if elif loop with read

I am trying to setup an if, elif statement that is dependant on a variable. If the user does not enter a valid option I would like it to prompt them again and start the loop over. Here is what I have so far: echo -n "enter variable (a, b, or c): " read freq if ; then echo "a" elif ;... (2 Replies)
Discussion started by: BStanley346
2 Replies

9. UNIX for Dummies Questions & Answers

Help with ELIF statement

I am receiving an elif error on line 13 and I can not figure out the reasoning behind it. I have added the then statement that I was initially missing. Any help would be great. #The purpose of this script is for the end user to be able to enter a positive number #User enters a number NUM=$1... (4 Replies)
Discussion started by: Brewer27
4 Replies

10. Shell Programming and Scripting

If..elif..else...fi

Hi all, I got some problems on executing the following scripts. Scripts: if ]; then echo "M${str}O 0 1" >> ${tempFile} elif ]; then echo "M${str}O 1 0" >> ${tempFile} else echo "M${str}O 0 0" >> ${tempFile} fi Error: "`;' is not expected." what's the problem? (2 Replies)
Discussion started by: Rock
2 Replies
Login or Register to Ask a Question