Find the answer to your Linux question:
Results 1 to 7 of 7
hi i am just learning to write shell scripts... i have a small problem with my concepts ? i mean in c and c++ if(of anything not equal to zero ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Mar 2005
    Location
    Bangalore, INDIA
    Posts
    122

    shell script


    hi
    i am just learning to write shell scripts...
    i have a small problem with my concepts ?

    i mean in c and c++ if(of anything not equal to zero ) is evaluated as true

    how are expressions evaluated using test
    eg :-
    $ x=5,y=7;
    $ if test $x -ne $y ; then
    echo "not equal "
    fi
    what value does test returns to if ..............
    bcoz exit status of test is 0 (ie echo $?)
    and if(0) should be false .
    i am confused ,the question might not make sense reply just incase u understand what
    i am trying to say .................
    by the way i was bash
    thank you .
    Portability is for people who cannot programme

  2. #2
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    well 0 is a successful exit status, and is the only successful exit status. As for your other questions, can you clarify?

  3. #3
    Linux Newbie
    Join Date
    Mar 2005
    Location
    Bangalore, INDIA
    Posts
    122

    hi

    clarification
    when does if get evaluated as true.....
    i mean is
    if [ 0 ] true
    or
    if [ 1 ] true

    thank you
    Portability is for people who cannot programme

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    Pretty sure it's 0

  6. #5
    Linux Engineer
    Join Date
    Nov 2004
    Location
    home
    Posts
    796
    Shell if is a bit hard to understand at first. Not at all like c if. First off, true in *nix is 0, not 1. Both 0 and 1 (and any number really) will return 0, since they are actual numbers, so they are true:

    Code:
    ray@jail:~$ test 1 ; echo $?
    0
    ray@jail:~$ test 0 ; echo $?
    0
    But if you tried to say, test for a directory which does not exist, it will return 1, so it is false:

    Code:
    ray@jail:~$ ls apples
    ls: apples: No such file or directory
    ray@jail:~$ test -d apples ; echo $?
    1
    Now if you compared 2 numbers, you would get the expected result:

    Code:
    ray@jail:~$ test 4 -eq 5 ; echo $?
    1
    ray@jail:~$ test 4 -eq 4 ; echo $?
    0

  7. #6
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    Mohit which particualr shell are you talking about ? Can
    you give us examples with a correct working syntax which
    illustrate your question ?

  8. #7
    Linux Newbie
    Join Date
    Mar 2005
    Location
    Bangalore, INDIA
    Posts
    122
    Quote Originally Posted by lakerdonald
    Pretty sure it's 0
    thank you problem solved
    Portability is for people who cannot programme

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •