Doubt in integer comparison


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Doubt in integer comparison
# 1  
Old 12-07-2011
Doubt in integer comparison

Hi
What is the difference between following commands

Command1
Code:
length=1
if [[ $length > 15 ]] ; then
   echo "Hellow world"
fi

Command 2
Code:
length=1
if [[ $length -gt 15 ]] ; then
   echo "Hellow world"
fi

which is correct usage from this

Moderator's Comments:
Mod Comment Please use code tags!

Last edited by zaxxon; 12-07-2011 at 07:28 AM.. Reason: code tags
# 2  
Old 12-07-2011
Both would work on a recent version of bash, probably a version more than 3.

Otherwise use the old syntax:
Code:
if [ $x -gt 5 ]
then
    echo "hello"
fi

# 3  
Old 12-07-2011
The [[ ]] syntax is a Conditional Expression and is described in the "Conditonal Expressions" section of your Shell Manual.

The [ ] syntax is a Test and is described in the "test" Shell command which is usually a superset of the unix "test" command.

Though there is much overlap in the syntax, the two forms are not interchangeable.

Code:
Beware of this classic mistake:
a=1
b=2
if [ $a > $b ]
then
        echo ok
else
        echo not ok
fi

Replies "ok" and CREATES A FILE CALLED "2" !

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] String integer comparison

I am trying to execute something like this file=/tmp/test.txt firstline=$(head -n 1 $file) value=`echo $firstline | cut -d'=' -f2` if then echo true fi i read the first line of a file, cut to the numeric value in the first line and check if it greater than 2 but for some... (11 Replies)
Discussion started by: madhan_dc
11 Replies

2. Shell Programming and Scripting

how to compare string integer with an integer?

hi, how to I do this? i="4.000" if ; then echo "smaller" fi how do I convert the "4.000" to 4? Thanks! (4 Replies)
Discussion started by: h0ujun
4 Replies

3. UNIX for Dummies Questions & Answers

Counting vowels in string. "Comparison pointer-integer".

I'm trying to write a programme which scans strings to find how many vowels they contain. I get an error saying that I'm trying to compare a pointer and an integer inif(*v == scanme){. How can I overcome this ? Also, the programme seems to scan only the first word of a string e.g.: if I type "abc... (1 Reply)
Discussion started by: fakuse
1 Replies

4. Programming

warning: comparison between pointer and integer

Hi guys :D I am still playing with my C handbook and yes, as you can see I have small problem as always :cool: I wrote a C code #include <stdio.h> #define MESSAGE 100 int main(void) { char input_mes - Pastebin.com And when I try to compile it I get following errors from gcc ... (1 Reply)
Discussion started by: solaris_user
1 Replies

5. Solaris

How to Use a Variable as Integer?

hello, i am writing a script that takes the UID from the PASSWD and then i want to increse the Number by one. for the Next user. i cannot get this to work that a variable is as interger example: set i = 0 set $i = $+1 it's in tcsh if it's mather (10 Replies)
Discussion started by: shatztal
10 Replies

6. Shell Programming and Scripting

integer comparison in ksh

Hi, I am just trying to compare integer in ksh. can you please tell me what's wrong with this code... or give me suggestions on alternative. sample code: i=0; if ; then echo inside if fi Thanks in advance! (6 Replies)
Discussion started by: nram_krishna@ya
6 Replies

7. Shell Programming and Scripting

Regex for for integer from 0-7 except 2

Hi, I am trying to use a regular exp to match any number between 0 and 7 except 2...... I am using: echo $myvar | egrep "{1}{1}" Which isnt working.... Please can someone help? Thanks yonderboy (10 Replies)
Discussion started by: yonderboy
10 Replies

8. Shell Programming and Scripting

Comparison doubt..

Hi, /etc/fstab file contains: ================== cluster_root#root / advfs rw 0 1 cluster_usr#usr /usr advfs rw 0 2 cluster_var#var /var advfs rw 0 2 /proc /proc procfs rw 0 0 t_d#t_f /t_m advfs rw 0 2 /dev/disk/dsk1c /ufs_mnt ufs ... (3 Replies)
Discussion started by: mansa
3 Replies

9. UNIX for Dummies Questions & Answers

Integer check (again)...

I have search the forum for an easier way to write this code. I have two separate 'if' to do this and it works but am wondering if someone knows a quick way to combine them. I want anything between 1 and 100 but not '01' or '005', '0010', etc. if ) ]] || ]; then echo "Try... (3 Replies)
Discussion started by: giannicello
3 Replies

10. Programming

comparison between pointer and integer

I received a warning when I tried to compile my program that said: warning: comparison between pointer and integer Could you please explain to me what this means, and in what ways I could possibly fix this? Thanks for your help! (2 Replies)
Discussion started by: sjung10
2 Replies
Login or Register to Ask a Question