Find the answer to your Linux question:
Results 1 to 6 of 6
Hi all, I'm executing the if-then-else block of code in a bash shell script. The following is the code. Code: #!/bin/bash grep -il error /tmp/*.nav echo $? if [ "$?" ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2007
    Posts
    11

    If-then-else (Help)


    Hi all,

    I'm executing the if-then-else block of code in a bash shell script. The following is the code.

    Code:
    #!/bin/bash
    
    grep -il error /tmp/*.nav
    
    echo $?
    
    if [ "$?" -eq  0 ]
    
    then
            echo "Errors present in streams log"
    else
            echo "Errors NNNNNOT present in streams log"
    fi
    In both cases ('then' and 'else'), the same echo text "Errors present in streams log" is being displayed. Although, the correct exit code is being displayed. Below is the output:

    Code:
    # ./test1.sh
    /tmp/abc.nav
    0
    Errors present in streams log
    
    # ./test1.sh
    1
    Errors present in streams log
    #
    I'm puzzled as to why this is happening. And for the life of me, I'm not able to figure what is wrong with the code. Any help is greatly appreciate.

    Regards,

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,379
    This is because the conditional test in the if statement is getting the return value from the echo command, which will always be 0 in your case. So, move the echo (if you want it) into each branch of the if/else/fi block. IE,
    Code:
    #!/bin/bash
    
    grep -il error /tmp/*.nav
    
    if [ "$?" -eq  0 ]
    then
            echo "Errors present in streams log"
    else
            echo "Errors NNNNNOT present in streams log"
    fi
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Jun 2011
    Location
    Belgium
    Posts
    84
    "$?" in your `if [ "$?" -eq 0 ]` is the return value of your last command `echo $?` and not of the grep command.

    one possible solution:
    Code:
    #!/bin/bash
    
    grep -il error /tmp/*.nav
    typeset -i ret="$?"
    echo $ret
    
    if [ "$ret" -eq  0 ]
    then
            echo "Errors present in streams log"
    else
            echo "Errors NNNNNOT present in streams log"
    fi

  4. #4
    Just Joined!
    Join Date
    Jun 2011
    Location
    Belgium
    Posts
    84
    Sorry Rubberman,

    our answers have crossed each other.

  5. #5
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,379
    Quote Originally Posted by kdg1955 View Post
    Sorry Rubberman,

    our answers have crossed each other.
    No problem! I considered something like yours, but I thought I'd just keep it simple!
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  6. #6
    Just Joined!
    Join Date
    Aug 2007
    Posts
    11
    Silly me. Didn't realize that. Many thanks Rubberman and kdg1955. That helped.

Posting Permissions

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