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.
Bash script errors out; there are '\r' characters in the execution trace
Line Endings are different on Windows or Linux.
Considering a file
Line A
Line B
Libe C
On Windows it will actually be like
Line A\r\n
Line B\r\n
Libe C\r\n
And on Linux it will be like
Line A\n
Line B\n
Libe C\n
As you can see there is a difference on how the end of a line is defined.
Apparently, you saved your file with Windows Line Ending \r\n
so as Linux does not "care" about \r
it assumes it's part of the script you are coding... so you are actually executing
#!/bin/bash\r
rsync -trnv /mnt/d/project/bundle/ /mnt/d/bundle/ --exclude=".git" --delete\r
cd /mnt/d/bundle/\r
git add .\r
read -p "" msg\r
git commit -m $msg\r
git push origin master\r
cd "${0%/*}"\r
and that will lead to various problems.
To solve this open the file in an editor and change the line endings.
e.g. in Notepad++
- Open File
- Edit -> Eol Conversion -> Unix (LF)
- Save File
[: missing `]' error in Unix | shell Script
I got the solution
memuse=$ free -m
if [ $memuse > 80 ];
then
echo "Attention: memory utilisation is high on $(hostname) at $(date)"
else
echo "Attention : memory utilisation is normal on $(hostname) at $(date)"
fi
This is my first time doing bash scripting. So the issue majorly due to the Indentation.
How to catch Spark error from shell script
try like this below example ...
your shell script can catch error code like this... where non zero exit code is error
$?
is the exit status of the most recently executed command; by convention, 0 means success and anything else indicates failure.
spark-submit transform_json.py
ret_code=$?
if [ $ret_code -ne 0 ]; then
exit $ret_code
fi
You have to code to return exit code by sys.exit(-1)
in error condition. check this for python exception handling...
Check this Exit codes in Python
Exception handling in shell scripting?
There is not really a try/catch
in bash (i assume you're using bash), but you can achieve a quite similar behaviour using &&
or ||
.
In this example, you want to run fallback_command
if a_command
fails (returns a non-zero value):
a_command || fallback_command
And in this example, you want to execute second_command
if a_command
is successful (returns 0):
a_command && second_command
They can easily be mixed together by using a subshell, for example, the following command will execute a_command
, if it succeeds it will then run other_command
, but if a_command
or other_command
fails, fallback_command
will be executed:
(a_command && other_command) || fallback_command
Related Topics
Shell Script, Saving the Command Value to a Variable
How to Increase the Scrollback Buffer in a Running Screen Session
Check If Directory Mounted with Bash
Add Text to File at Certain Line in Linux
How to Capture the Output of a Top Command in a File in Linux
What Is the "Current" in Linux Kernel Source
Check If Vt-D/Iommu Has Been Enabled in The Bios/Uefi
How to Build and Compile Cross Platform Xamarin Apps on Linux
Shell Shift Procedure - What Is This
How to Get Multi-Line String Between Two Braces Containing a Specific Search String
Why Does Find -Exec Mv {} ./Target/ + Not Work
What's an Alternative for Dtrace on Linux
What Is the Command to Match Brackets in Emacs
Unshare --Pid /Bin/Bash - Fork Cannot Allocate Memory