Linux - Capture exit code of a ruby script [duplicate] - ruby

This question already has answers here:
Exit Shell Script Based on Process Exit Code [duplicate]
(9 answers)
Is it possible to get the exit code from a subshell?
(3 answers)
Closed 8 years ago.
I have a simple ruby script which uses the abort function to exit with a non-zero exit code
#!/usr/bin/env ruby
puts "I ran"
abort "Exiting"
How can I capture the exit code when I execute this command in bash?
I have tried exit_code=./test or exit_code=ruby test to no avail.
Thanks

Try this:
./test
echo $?
The special shell variable $? contains the exit code of the last terminated program.
It does not matter whether your program is a ruby program. All Unix programs have an exit code which is handled alike in the starting shell.

The exit code of the last program that ran is stored in $?

You find the exit code from the previously executed command in the variable $?.

Related

Exit code from child script to parent script [duplicate]

This question already has answers here:
unix command line execute with . (dot) vs. without
(5 answers)
Comparing numbers in bash scripting
(4 answers)
Closed 1 year ago.
I have the below parent script which calls a child script.
parent.sh
#!/bin/bash
export home=`pwd`
echo "calling child"
. ${home}/bin/child.sh
retn_code=$?
if (retn_code -ne 0)
then
exit $retn_code
else
echo "successful"
fi
exit 0
child.sh:
#!/bin/bash
exit 1
When I execute the parent script, the exit status of 1 is not getting captured in parent script. The last line of the log printed is "calling child" and there are no lines printed after that in log.
Is there something I am missing while getting the exit status from child to parent?
. does not call the child, it runs its code. So if the child script calls exit, the parent exits. You want to do:
#!/bin/bash
home=$(pwd)
echo "calling child"
if "${home}"/bin/child.sh; then
echo success
else
exit # status returned by child will propagate
fi
exit 0
Note that it's very odd to use the variable home here. If you want it defined in child.sh, IMO it would be clearer to write if home=$(pwd) ./bin/child.sh; then .... Your usage of export implies that you want it defined in child.sh, but I suspect you don't actually want that. It's not pertinent to the question, so I've just removed what I believe is an unnecessary export.

Could the shell function return value not exceed 255? [duplicate]

This question already has answers here:
ExitCodes bigger than 255, possible?
(3 answers)
Return value range of the main function
(7 answers)
Closed 4 years ago.
#!/usr/bin/env bash
# set -x
readonly err_code=256
my_function () {
return $err_code
}
my_function
echo $? # print 0
If the err_code exceed the 255, all the top bits will be dropped just like unsigned byte, how to explain this? Is there any feature document for this? I googled a lot w/o luck.
Thanks!
UPDATE:
Okay, I got it, it doesn't just happen only in shell but Unix-based system, the shell function is also called by command substitution.
Thanks for linked questions!
If you look at man bash and search for EXIT STATUS you will find the following explanation:
EXIT STATUS
The exit status of an executed command is the value returned by the waitpid system call or equivalent
function. Exit statuses fall between 0 and 255, though, as explained below, the shell may use values
above 125 specially. Exit statuses from shell builtins and compound commands are also limited to this
range. Under certain circumstances, the shell will use special values to indicate specific failure modes.
For the shell's purposes, a command which exits with a zero exit status has succeeded. An exit status of
zero indicates success. A non-zero exit status indicates failure. When a command terminates on a fatal
signal N, bash uses the value of 128+N as the exit status.
If a command is not found, the child process created to execute it returns a status of 127. If a command
is found but is not executable, the return status is 126.
If a command fails because of an error during expansion or redirection, the exit status is greater than
zero.
Shell builtin commands return a status of 0 (true) if successful, and non-zero (false) if an error occurs
while they execute. All builtins return an exit status of 2 to indicate incorrect usage, generally
invalid options or missing arguments.
Bash itself returns the exit status of the last command executed, unless a syntax error occurs, in which
case it exits with a non-zero value. See also the exit builtin command below.
If you really want to return values greater than 125, you can use echo instead of return like this:
#!/usr/bin/env bash
my_function () {
echo 256
}
retval=$( my_function )
echo $retval

`set` options in shell [duplicate]

This question already has answers here:
Automatic exit from Bash shell script on error [duplicate]
(8 answers)
Specific man page for shell built-in commands like 'source' [duplicate]
(1 answer)
Closed 4 years ago.
I often see set -e or set -ex in dockerfiles and I was wondering what their purpose was. Recently, I've also seen set -o pipefail, and I have no idea what it does. I tried man set to see if there was any manual description, but there wasn't any and I am resorting to ask here.
You can find the full list here:
https://ss64.com/bash/set.html
-e
Exit immediately if a simple command exits with a non-zero status, unless the command that fails is part of an until or while loop, part of an if statement, part of a && or || list, or if the command's return status is being inverted using !. -o errexit
-x
Print a trace of simple commands and their arguments after they are expanded and before they are executed. -o xtrace
-o pipefail
If set, the return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands in the pipeline exit successfully. This option is disabled by default.

How to handle sub-command errors in bash script? [duplicate]

This question already has answers here:
Why does "local" sweep the return code of a command?
(2 answers)
Why is bash errexit not behaving as expected in function calls?
(4 answers)
Closed 4 years ago.
I want to know how best to exit a script when an error occurs within a sub-command - specifically, in an assignment (i.e., of the form MYVAR="$(...)").
The minimal example of my problem is the following bash script.
#!/bin/bash
set -e
fail() {
echo "Some error" >&2
exit 1
}
main() {
local my_val="$(fail)"
echo 'Success!'
}
main
This will output the following:
Some error
Success!
What I am trying to figure out is how best to detect and handle the failure which occurs so that the Success stage is never reached.

What is $? var in script below on condition of while loop [duplicate]

This question already has answers here:
What is the $? (dollar question mark) variable in shell scripting? [duplicate]
(9 answers)
Closed 9 years ago.
#!/bin/sh
host google.com>/dev/null
while [ $? -ne 0 ];
do
sleep 3
done
say "You are now connected to internet"
I guess $? is associated with google.com>/dev/null, making the logic work, but i am interested in detail description on $?
Thanks in advance.
I know this will sound pedantic, but $? is not a variable, it is a value. ? is the name of the variable, placing $ at the front gives the value.
Now you can search for ? in man bash:
Expands to the exit status of the most recently executed foreground pipeline.
It is often tested unnecessarily. if and while statements in bash (and most shells) test for success(true) or failure(false). Success is where the value of ? is 0, and failure when it is some other number (which has the range 1-255). ! means "not", as in many languages, and inverts the truth:
while ! host google.com>/dev/null
do
sleep 3
done
echo "You are now connected to internet"
(I had to use echo, not sure where say comes from, Perl?)

Resources