0

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 ?

  • 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 May 17 '17 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 May 17 '17 at 11:43
  • Actually I have used same "if" and paylodjob.sh doesn't return anything – ProgBeginner May 17 '17 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 May 17 '17 at 11:51
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
  • So this means whatever exit status I am getting is the exit status of last command within paylodjob.sh – ProgBeginner May 17 '17 at 11:53
  • @unixbeginner Correct. – Kusalananda May 17 '17 at 11:53
  • Thanks @Kusalananda it was same issue that you pointed out – ProgBeginner May 17 '17 at 11:55
  • @unixbeginner you could also shorten that to sh payload.sh && echo "Success" || echo "Failure" if you like. – terdon May 17 '17 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.