I am using the bash script wait for it to check if a container is up and running before I launch another container.
https://github.com/vishnubob/wait-for-it
It seems that if it's successful it returns 0 and if not 124. Is it possible to check this value and if it's not successful exit the script?
I've tried
./wait-for-it.sh $BROKER_ADDRESS
echo $?
if ($?==124)
then
echo "exiting as broker service never became available"
exit
fi
And I can see it echos 124 in my terminal but then the check fails and it carries on launching the container. I'm assuming my conditional check is wrong but I can't seem to figure out why
$? Always returns the exit status of the previous command
so in this case in your 3rd line in if loop you are comparing exit status of the echo command in your second line to 124. So either remove the second line or store the exit status to a variable & use that variable in your if loop.
Related
I use the following pattern often:
some_long_running_command && echo success
However, sometimes I forget to attach the && success, so I write out the status check in the next line:
some_long_running_command
[ $? -eq 0 ] && echo success
As you can see, the status check in the second example ([ $? -eq 0 ] &&) is much more verbose than the check from the first example (&&).
Is there a more concise way to run a command only if the previous command completes successfully, when the previous command is already running?
exit exits its shell using the same exit status as the most recently completed command. This is true even if that command runs in the parent of the shell that executes exit: you can run exit in a subshell.
some_long_running_command
(exit) && echo success
Depending on the command, you can also suspend the job, then resume it with fg, which will have the same exit status as the command being resumed. This has the benefit of not having to wait for the command to complete before adding the new && list.
some_long_running_command
# type ctrl-z here
fg && echo success
Generally, this will work as long as some_long_running_command doesn't have to keep track of some resource that is changing in real-time, so that you don't lose data that came and went while the job was (briefly) suspended.
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
I am writing a script that executes around 10 back-end processes in sequence, depending on if the previous process was executed without any errors.
Now let's assume the scenario, in which lets say 5th process failed and script came out. But I want to code it in a way such that, when next time user runs it(after removing the error because of which script exited last time), he should be able to run from 5th process onwards and not again from 1st process.
To be more specific, assume following is the script:
Script Starts
Process1
if [ $? -eq 0 ] then
Process2
if [ $? -eq 0 ] then
Process3
if [ $? -eq 0 ] then
..
..
..
..
if [ $? -eq 0 ] then
Process10
else
exit
So here the script will exit anytime if any one of the process fails to complete with status 0. So again, if process5 fails, and user corrects the problem and restarts script, the script should start with process5 again and not process1 or at least there should be an option to user if he wants to resume the script or start it back from beginning i.e. process1.
What all possible ways we can code this kind of script, also please bear in mind, I am not allowed to use a temporary db, where I can store the status of each process.
I need to code in sh (shell script) in unix.
A simple solution would be to write stamp files:
#/bin/sh
set -e # Automatically abort if any simple command fails
if ! test -f cmd1-stamp; cmd1; fi
touch cmd1-stamp
if ! test -f cmd2-stamp; cmd2; fi
touch cmd2-stamp
When the script executes, if cmd1-stamp exists, cmd1 is not executed. Otherwise, cmd1 is executed. The script will abort if it fails. Note that it is very tempting to write test -f cmd1-stamp || cmd1, and this seems to work ( in bash ) but the shell specs state that the shell shall abort if the simple command that fails is not a part of an AND or OR list, and I suspect this is (yet another) instance of bash not conforming to the spec. (Although it doesn't seem to specify that the shell shall not abort if the failing command is part of an AND or OR list.)
I use SSH Secure Shell client to connect to a server and run my scripts.
I want to stop a script on some conditions, so when I use exit, not only the script stops, but all the client disconnects from the server!, Here is the code:
if [[ `echo $#` -eq 0 ]]; then
echo "Missing argument- must to get a friend list";
exit
fi
for user in $*; do
if [[ !(-f `echo ${user}.user`) ]]; then
echo "The user name ${user} doesn't exist.";
exit
fi
done
A picture of the client:
Why is this happening?
You use source to run the script, this runs it in the current shell. That means that exit terminates the current shell and with that the ssh session.
replace source with bash and it should work, or better put
#!/bin/bash
on to of the file and make it executable.
exit returns from the current shell - If you've started a script by running it directly, this will exit the shell that the script is running in.
return returns from a function or sourced file (TY Dennis Williamson) - Same thing, but it doesn't terminate your current shell.
break returns from a loop - Similar to return, but can be used anywhere within a loop to stop processing more items. This is probably what you want.
if you are running from the current shell, exit will obviously exit from the shell and disconnect you. try running it in a new shell ( use a . before the script) or else use 'return' instead of exit
I am running a application form the shell script. Now I like to know the exit status of the application to know whether it exit normally or abnormally ( crash etc). How I know it?
Example:
./mytestApp
Bash stores the last process' exit value in the special variable $?.
You can use special variable $? which contains an exit status of the last comand.
$? contains the exit status of the last command executed. So, if the last command was ./mytestapp, $? would contain its exit status immediately after (note that you can only retrieve this value once and it must be retrieved immediately after the command whose exit status you want to know). You may want to capture it in a variable, e.g.
#!/bin/bash
./mytestapp
APPSUCCESS=$?
# Continue doing whatever it is you're doing
This all assumes that you're using bash (sh and zsh will work as well, IIRC).
The special variable $? will contain the exit status of the last command in bash.