1

Hi I am calling a shell script from inside another shell script and if that script call goes successful I have to proceed further and I am using sh command to call another script like :

sh payloadjob.sh
if [[ $? -eq 0 ]]; then
    echo Success message 
else
    echo Failure message
fi

But I am noticing that even script call go successful it gives Exit status as 1 however it should return 0 for success why it is happening and how do I come to know that script call via sh command is successful or not ?

4
  • I edited your question a bit to change the shell keywords to the correct case. If is different from if. I assume that wasn't the issue here, since you would have got a syntax error (or an error about If not found)
    – ilkkachu
    Commented May 17, 2017 at 11:42
  • But FWIW, that looks like it should work in Bash or others with [[ ]]. Now the question just is, if payloadjob.sh even returns a sane return value.
    – ilkkachu
    Commented May 17, 2017 at 11:43
  • Actually I have used same "if" and paylodjob.sh doesn't return anything Commented May 17, 2017 at 11:48
  • @unixbeginner Um, it does return an exit status. The question is, why is it not returning the exit status that you expect?
    – Kusalananda
    Commented May 17, 2017 at 11:51

1 Answer 1

3

If the payload.sh script is not exiting with an explicit exit 0 or exit 1, then the exit status of it will be that of the last command in the script.

If this last command does, for example, rm something 2>/dev/null, then the exit status will be non-zero if something can not be found or removed.

In the script that you have shown, you are correctly testing the $? variable. You may also simplify the test to the equivalent

if sh payload.sh; then
    echo "Success"
else
    echo "Failure"
fi
4
  • So this means whatever exit status I am getting is the exit status of last command within paylodjob.sh Commented May 17, 2017 at 11:53
  • @unixbeginner Correct.
    – Kusalananda
    Commented May 17, 2017 at 11:53
  • Thanks @Kusalananda it was same issue that you pointed out Commented May 17, 2017 at 11:55
  • @unixbeginner you could also shorten that to sh payload.sh && echo "Success" || echo "Failure" if you like.
    – terdon
    Commented May 17, 2017 at 12:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .