Exit code from child script to parent script [duplicate] - bash

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.

Related

Can someone explain how unix exit commands work?

I have read about unix exit commands but please can someone tell me how they work exactly.
I mean what is their purpose and how can they be used.
Also i see people talking about success = 0 or something and i dont have a clue what they mean by this.
the "exit" command exits the shell script
echo "A"
exit 1
echo "B"
In the above example 'echo "B"' is not executed because of the exit statement.
It's like a return statement in normal progemming languages. The expression after the exit is the return value. Convention is that 0 means "Success" other values means an error.
So if the above script is called q.sh, than this script can be called from an other script:
sh ./q.sh
echo $?
The code "$?" means "exit" value of the last shell script.
Above script prints "1"

shell script execution successful but output has errors, how to determine error and exit main script?

I have a main script. Inside it I call other three shell scripts, A,B and C. All were successful. Exit codes are all equal to zero. However, when I looked into the output file of the first script which is A, it contains an error message. Now I want to exit the main script and not to continue running the other scripts after the script that has output error. Can anyone help me on this? Thanks!
Even if some command in your first bash script results in an error, the script as a whole may complete with exit code 0.
You can check the exit code of any individual command in your script by using the $? variable. This variable stores the exit code of the previous command. This will allow you to check for errors within the script.
The easiest way is to append || exit 1 to the statement which is throwing the error. That will cause the script to exit if the exit code of the command is 1 (i.e. an error).
So assuming you had a command sqlscript and you wanted the entire script to exit if sqlscript exited with a non-zero exit code you would do
sqlscript || exit 1
As a point of trivia, the 1 in exit 1 is not needed. A plain exit command would also exit with the exit status of the last executed command.
Which would be false (code=1) if the sqlscript command fails. If the sqlscript command succeeds, the exit code is the exit code of sqlscript. In that case, the || does not trigger and the exit command is not executed.
I have a main script. Inside it I call other three shell scripts, A,B
and C. All were successful. Exit codes are all equal to zero. However,
when I looked into the output file of the first script which is A, it
contains an error message. Now I want to exit the main script and not
to continue running the other scripts after the script that has output
error.
Since script A doesn't return an error exit code, you have to inspect its output. This is quite easy with grep provided that you have a search string which clearly identifies an error message, e. g.:
# this echo command simulates script A - it outputs "error" and exits with 0:
echo "contains an error message" >StoreKey_All.csv # assumed this output file
grep error StoreKey_All.csv && exit 1 # exit if output has error
# continue with scripts B and C
echo B

How to terminate execution of a fish script from a sourced file?

The exit command when executed from a file being sourced doesn't terminate the execution of the program where it was being sourced, how to do this? Consider this files for a clearer explanation:
a.fish:
source b.fish
echo "This should never run!"
b.fish:
echo "Failing now"
exit 1
This will result in this (undesired output):
Failing now
This should never run!
And the exit status is 0! Is there a solution for B to terminate execution of A as if exit was written in A itself?
It's perfectly working the way you want with bash and zsh. Still, I found a solution for fish:
source b.fish; or exit 1
This will exit a.fish if b.fish exited with exit 1, and will continue otherwise.

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

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 $?.

Bash: How to lock also when perform an outside script

Here is my bash code:
(
flock -n -e 200 || (echo "This script is currently being run" && exit 1)
sleep 10
...Call some functions which is written in another script...
sleep 5
) 200>/tmp/blah.lockfile
I'm running the script from two shells successively and as long as the first one is at "sleep 5" all goes good, meaning that the other one doesn't start. But when the first turns to perform the code from another script (other file) the second run starts to execute.
So I have two questions here:
What should I do to prevent this script and all its "children" from run while the script OR its "child" is still running.
(I didn't find a more appropriate expression for running another script other than a "child", sorry for that :) ).
According to man page, -n causes the process to exit when it fails to gain the lock, but as far as I can see it just wait until it can run. What am I missing ?
Your problem may be fairly mundane. Namely,
false || ( exit 1 )
Does not cause the script to exit. Rather, the exit instructs the subshell to exit. So change your first line to:
flock -n -e 200 || { echo "This script is currently being run"; exit 1; } >&2

Resources