How to Output Return Code in Shell

How to output return code in shell?

(/bin/sh -c "myscript.sh" >log.txt 2>&1 ; echo $? >somefile) & echo $!

Get exit status of a process in bash

You can simply do a echo $? after executing the command/bash which will output the exit code of the program.

Every command returns an exit status (sometimes referred to as a return
status or exit code). A successful command returns a 0, while an
unsuccessful one returns a non-zero value that usually can be interpreted
as an error code. Well-behaved UNIX commands, programs, and utilities return
a 0 exit code upon successful completion, though there are some exceptions.

# Non-zero exit status returned -- command failed to execute.
echo $?

echo

# Will return 113 to shell.
# To verify this, type "echo $?" after script terminates.
exit 113

# By convention, an 'exit 0' indicates success,
# while a non-zero exit value means an error or anomalous condition

Alternately if you wish to identify return code for a background process/script (started with nohup or run in background & operator) you could get the pid of the process/script started and wait for it to terminate and then get the exit code.

# Some random number for the process-id is returned
./foo.sh &
[1] 28992

# Get process id of the background process
echo $!
28992

# Waits until the process determined by the number is complete
wait 28992

[1]+ Done ./foo.sh

# Prints the return code of the process
echo $?
0

Also take a look at Bash Hackers Wiki - Exit Status

Capturing output and exit codes in BASH / SHELL

It's possible to capture the segfault error message, but you really need to work at it.

Here's one way:

outputA=$(bash -c '(./a)' 2>&1)

Here we create an child shell (with bash -c) whose stderr is redirected to stdout, and then get that child to execute the program in an explicit subshell. Errors inside the subshell will be captured by the child bash, which will then generate an error message (which is not quite the same as the message produced by an interactive bash):

$ echo $outputA
bash: line 1: 11636 Segmentation fault (core dumped) ( ./a )

bash - how to process exit code when using set -e and get output of command

You need to handle the exit code of the command.

The exit status of the command is the exit status of last command executed.

Variable assignment is not a command.

So you can just do what's popular in set -e scripts:

formoutput=$(yad ...) || ret=$? && ret=$?

to catch the return value.

Or you can ignore the exit by invoking : command:

formoutput=$(yad ...) ||:

The ||: is really || with : command. The colon command returns exit status of zero, the exit status of the list of commands a || b is the exit status of the last command executed in the list, as : always returns zero, the list of commands will also return zero exit status.

Or use if:

if ! formoutput=$(yad ...); then 
echo "AAAA! yad failed! abort ship!" >&2
fi

Note that if command also has an exit status of the last command executed. So be aware that:

if true; then
false
fi

will exit from your set -e script.

Returning value from called function in a shell script

A Bash function can't return a string directly like you want it to. You can do three things:

  1. Echo a string
  2. Return an exit status, which is a number, not a string
  3. Share a variable

This is also true for some other shells.

Here's how to do each of those options:

1. Echo strings

lockdir="somedir"
testlock(){
retval=""
if mkdir "$lockdir"
then # Directory did not exist, but it was created successfully
echo >&2 "successfully acquired lock: $lockdir"
retval="true"
else
echo >&2 "cannot acquire lock, giving up on $lockdir"
retval="false"
fi
echo "$retval"
}

retval=$( testlock )
if [ "$retval" == "true" ]
then
echo "directory not created"
else
echo "directory already created"
fi

2. Return exit status

lockdir="somedir"
testlock(){
if mkdir "$lockdir"
then # Directory did not exist, but was created successfully
echo >&2 "successfully acquired lock: $lockdir"
retval=0
else
echo >&2 "cannot acquire lock, giving up on $lockdir"
retval=1
fi
return "$retval"
}

testlock
retval=$?
if [ "$retval" == 0 ]
then
echo "directory not created"
else
echo "directory already created"
fi

3. Share variable

lockdir="somedir"
retval=-1
testlock(){
if mkdir "$lockdir"
then # Directory did not exist, but it was created successfully
echo >&2 "successfully acquired lock: $lockdir"
retval=0
else
echo >&2 "cannot acquire lock, giving up on $lockdir"
retval=1
fi
}

testlock
if [ "$retval" == 0 ]
then
echo "directory not created"
else
echo "directory already created"
fi

shell: capture command output and return status in two different variables

Just capture $? as you did before. Using the executables true and false, we can demonstrate that command substitution does set a proper return code:

$ output=$(true); rc=$?; echo $rc
0
$ output=$(false); rc=$?; echo $rc
1

Multiple command substitutions in one assignment

If more than one command substitution appears in an assignment, the return code of the last command substitution determines the return code of the assignment:

$ output="$(true) $(false)"; rc=$?; echo $rc
1
$ output="$(false) $(true)"; rc=$?; echo $rc
0

Documentation

From the section of man bash describing variable assignment:

If one of the expansions contained a command substitution, the exit
status of the command is the exit status of the last command
substitution performed. If there were no command substitutions, the
command exits with a status of zero.



Related Topics



Leave a reply



Submit