Exit from Shell script without executing anything - shell

I have a shell script installed to run every day.
The script consists of some rm (delete) commands and SCP commands for file transfer. The script is failing due to some errors in production. I want the job to return exit code as 0 without executing anything, so I wrote
exit 0;
at the beginning of the shell script so that as soon as the script executes, it will exit with out any commands after that exit 0; but still the script is failing.
I cannot edit the whole contents inside the file nor delete the script or contents inside the script.
Please suggest whether I am using the right syntax for exit command or how to make the script to exit with return code 0 as soon as it starts executing.

The ';' is unnecessary. You can exit like this:
exit
exit 1
This is a description about 'Exit and Exit Status'.

Related

Run a command right before a script exits due to failure

Let's say there's this script
#!/bin/zsh
python -c 'a'
which will fail since a isn't defined. Just before the shell script exits, I want to run a command, say echo bye. How can that be achieved?
Flow is to be:
Python command above fails.
bye appears in terminal.
The zsh script exits.
I'd prefer it to affect the python command as little as possible such as indent, putting it in an if block, checking its exit code etc. In real life, the command is in fact multiple commands.
In the script you posted, the fact that the shell exits is unrelated to any error. The shell would exit, because the last argument hast been executed. Take for instance the script
#!/bin/zsh
python -c 'a'
echo This is the End
The final echo will always be exeuted, independent of the python command. To cause the script to exit, when python returns a non-zero exit code, you would write something like
#!/bin/zsh
python -c 'a' || exit $?
echo Successful
If you want to exit a script, whenever the first one of the commands produces a non-zeror exit status, AND at the same time want to print a message, you can use the TRAPZERR callback:
#!/bin/zsh
TRAPZERR() {
echo You have an unhandled non-zero exit code in your otherwise fabulous script
exit $?
}
python -c 'a'
echo Only Exit Code 0 encountered

How to exit shell script with exit 0

I am trying to run .hql file through a shell script like below
#!/bin/bash
cd /path/
hive -f hive_script.hql
but the script 'hive_script.hql' is failing . I want to exit the shell script successfully even hive_script.hql script fails. Is it possible?
If you don't put an explicit exit in your script, the exit code of your script would be the exit code of the last command it ran - in your case, it is the hive -f ... command.
You can add exit 0 at the end of your script to make sure it always exits with zero.
Related:
Raise error in Bash script
If you want the script to exit with 0 even when hive -f hive_script.hql would fail, you can just or the command with something that won't ever throw an error
hive -f hive_script.hql || :
This means that if the hive command fails, bash should also run the second command. In this case, that command is :, which is basically pass from python, and will always return a 0 status.

Exiting a shell script with an error

basically I have written a shell script for a homework assignment that works fine however I am having issues with exiting. Essentially the script reads numbers from the user until it reads a negative number and then does some output. I have the script set to exit and output an error code when it receives anything but a number and that's where the issue is.
The code is as follows:
if test $number -eq $number >dev/null 2>&1
then
"do stuff"
else
echo "There was an error"
exit
The problem is that we have to turn in our programs as text files using script and whenever I try to script my program and test the error cases it exits out of script as well. Is there a better way to do this?
The script is being run with the following command in the terminal
script "insert name of program here"
Thanks
If the program you're testing is invoked as a subprocess, then any exit command will only exit the command itself. The fact that you're seeing contrary behavior means you must be invoking it differently.
When invoking your script from the parent testing program, use:
# this runs "yourscript" as its own, external process.
./yourscript
...to invoke it as a subprocess, not
# this is POSIX-compliant syntax to run the commands in "yourscript" in the current shell.
. yourscript
...or...
# this is bash-extended syntax to run the commands in "yourscript" in the current shell.
source yourscript
...as either of the latter will run all the commands -- including exit -- inside your current shell, modifying its state or, in the case of exit, exec or similar, telling it to cease execution.

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.

Resources