问题
I'm trying to call a custom shell script through sh
:
/bin/sh -c 'myscript.sh` >log.txt 2>&1 & echo $!
Output of this command is a PID of a created background process. I want to instruct /bin/sh
to save return code of myscript.sh
to some file. Is it possible?
回答1:
(/bin/sh -c "myscript.sh" >log.txt 2>&1 ; echo $? >somefile) & echo $!
回答2:
echo $? >> /path/to/return_code
$? has the return code of the last statement in bash.
回答3:
(
/bin/sh -c 'myscript.sh` >log.txt 2>&1
echo $? > some_file
) &
来源:https://stackoverflow.com/questions/6810279/how-to-output-return-code-in-shell