Raise Error in a Bash Script

Raise error in a Bash script

This depends on where you want the error message be stored.

You can do the following:

echo "Error!" > logfile.log
exit 125

Or the following:

echo "Error!" 1>&2
exit 64

When you raise an exception you stop the program's execution.

You can also use something like exit xxx where xxx is the error code you may want to return to the operating system (from 0 to 255). Here 125 and 64 are just random codes you can exit with. When you need to indicate to the OS that the program stopped abnormally (eg. an error occurred), you need to pass a non-zero exit code to exit.

As @chepner pointed out, you can do exit 1, which will mean an unspecified error.

How to throw an error in Bash?

There are multiple ways to do something similar:

use subshells (might not be the best solution if you want to set parameters etc...)

(
if [[ "$status" -ne "200" ]]
then
exit 1
fi
) || (
# on error / where I want to get if status != 200
echo "error thrown"
)

use an intermediate error variable (you can catch multiple errors by setting different numbers. Also: less indentation depth)

if [[ "$status" -ne "200" ]]
then
error=1
fi

if [ $error != 0 ]
then
echo "error $error thrown"
fi

use immediately the exit value of your test (note that I changed -ne to -eq)

[[ "$status" -eq "200" ]] || echo "error thrown"

How to purposely throw an Error in the shell script

but i need something else instead of -cd 123 syntax

You could say:

exit 42

instead. This would make the script exit with a non-zero exit code that should be picked up by Jenkins.

help exit tells:

exit: exit [n]
Exit the shell.

Exits the shell with a status of N. If N is omitted, the exit status
is that of the last command executed.

How to make bash throw an error if -eq is used with non-numeric arguments inside [[ ... ]]?

-eq in [[ ... ]] evaluates its operands as arithmetic expressions, i.e. they behave like in ((...)). Variables with or without a dollar sign are expanded, but if their expansion is a valid identifier, the variable of that name is used to produce the value to compare.

#!/bin/bash
A=foo
B=bar
for foo in 0 1 ; do
for bar in 0 1 ; do
if [[ A -eq B ]] ; then
echo "$A ($foo) eq $B ($bar)"
else
echo "$A ($foo) ne $B ($bar)"
fi
done
done

This expansion happens recursively

#!/bin/bash
a=1 b=a c=b d=c e=d f=e g=f h=g i=h j=i k=j l=k m=l
n=m o=n p=o q=p r=q s=r t=s u=t v=u w=v x=w y=x z=y
[[ z -eq 1 ]] && echo ok

The recursion limit is 1024:

#!/bin/bash
(
echo -n a=
for var in {a..z}{a..z}{a..z} ; do
echo $var
echo -n $var=
done
echo 42
echo '[[ a -eq 42 ]] && echo ok'
) | bash

Capture shell output after running python script that raised Exception

Python writes exceptions to standard error, hence if you want to capture them along with the normal output, you should redirect stderr to stdout by adding 2>&1 to the bash input.

In your case it would be bash("""python3 some_script.py 2>&1""").

I assume the bash function sends the argument to the bash shell.

Is there a TRY CATCH command in Bash

Is there a TRY CATCH command in Bash?

No.

Bash doesn't have as many luxuries as one can find in many programming languages.

There is no try/catch in bash; however, one can achieve similar behavior using && or ||.

Using ||:

if command1 fails then command2 runs as follows

command1 || command2

Similarly, using &&, command2 will run if command1 is successful

The closest approximation of try/catch is as follows

{ # try

command1 &&
#save your output

} || { # catch
# save log for exception
}

Also bash contains some error handling mechanisms, as well

set -e

it stops your script if any simple command fails.

And also why not if...else. It is your best friend.



Related Topics



Leave a reply



Submit