#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2006
    Posts
    319
    Rep Power
    9

    Shell programming question


    Hi,

    In shell programming, it says to use -eq for numeric comparision, but still both the following codes work...

    is it ok to use == for numeric comparison ? Also, do I need to enquote $b and $c in "" (eg: if [ "$b" -eq "$c"]?


    Code:
    b=5;
    c=89;
    
    if [ $b == $c ]
    then
    echo "EQUAL"
    else
    echo "NOT EQUAL"
    fi

    Code:
    b=5;
    c=89;
    
    if [ $b -eq $c ]
    then
    echo "EQUAL"
    else
    echo "NOT EQUAL"
    fi
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,450
    Rep Power
    1751
    If I know the variables/literals involved on either or both sides of a comparison are going to be numeric I tend to use the -eq, ne, -lt, etc. syntax. For 'other stuff' I use the = (single equal sign - which I believe is equivalent to ==) and I do wrap quotes around literals and variables to prevent the shell from having confusion should there be special characters or a variable being empty.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2006
    Posts
    319
    Rep Power
    9
    I will say shell script as an undefined language :O)


    Originally Posted by SimonJM
    If I know the variables/literals involved on either or both sides of a comparison are going to be numeric I tend to use the -eq, ne, -lt, etc. syntax. For 'other stuff' I use the = (single equal sign - which I believe is equivalent to ==) and I do wrap quotes around literals and variables to prevent the shell from having confusion should there be special characters or a variable being empty.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0
    its works either way but you should have use what's been said in the tut
    i think its the right way. not good at it though

IMN logo majestic logo threadwatch logo seochat tools logo