0

Shell script 1.sh is like

#/bin/bash

if some java command;
then 
exit 1;
else    
exit 0;
fi

Shell script 2.sh will determine its execution based on the result (1 or 0) from 1.sh

#/bin/bash
readyToDoSomethingIfOne=$(1.sh)

if($readyToDoSomethingIfOne=="1");
then
echo "ready to go";
else    
echo "Not ready yet" ;
fi

It looks like exit command from 1.sh does not pass the value to 2.sh. Is there any good way to do so?

By the way, 1.sh and 2.sh have to be separated for business reasons.

Thanks

3
  • The exit status is $?, not captured in $() (which gets content sent to stdout). In general, though, if you wanted to know if something succeeded or not, you don't need to check $? explicitly -- if some_command will take the true branch if $? is 0 after running some_command, the else branch otherwise. Commented Mar 21, 2014 at 19:03
  • By the way, bash scripts shouldn't be named with .sh extensions -- first, commands don't conventionally have extensions at all in UNIX (you don't run ls.elf); second, bash is not POSIX sh, and someone running sh foo.sh rather than bash foo.sh would be in for an unwelcome surprise if you used any features not available in the barebones standard form of the language. Commented Mar 21, 2014 at 19:05
  • 2
    By the way, exit 0 should be used on success, and exit 1 on failure. Commented Mar 21, 2014 at 19:10

3 Answers 3

2
#/bin/bash

if 1.sh; then
  echo "ready to go";
else    
  echo "Not ready yet" ;
fi
Sign up to request clarification or add additional context in comments.

2 Comments

While your answer is correct, you need to say if ! 1.sh in order to comply with OPs return convention. (Take a look at OPs script 1; it returns with an exit code of 1 in case the command is successful :) )
@devnull, I'm going to leave this as-is, and upvote your comment. Better for the OP to fix their code.
0

The construct

VAR=$(COMMAND)

assigns the output of COMMAND to the variable VAR. You can get the exit status of the last command from the automatic variable $?. That is

COMMAND
case $? in
  0)
      # do this
      ;;
  1)
      # do that
      ;;
  *)
      # do something else
      ;;
esac

If you only want to distinguish between success ($? == 0) and failure ($? != 0) then you can use the simpler if construct as in your script1.sh.

If using $?, watch out that it changes after every command, so if you need the value for later use, save it in a variable.

COMMAND
status=$?
# now use $status

Comments

0
#/bin/bash
if readyToDoSomethingIfOne=$(1.sh); then # Just use the exit status of the initial script directly.
    …
else
    echo 'nope!'
fi

If you don't actually need the variable at all, just run the script like this:

if ./1.sh; then
  echo "Not ready yet"
  return / exit / continue # Not enough context to know which exit approach is best.
fi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.