Error Bash Linux
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs bash exit 0 Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a bash if exit code community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Raise error in bash script up vote shell script error handling 6 down vote favorite 1 I want to raise an error in a Bash script with message "Test cases Failed !!!". How to do this in Bash ? For example: if [ condition ]; then raise error "Test cases failed !!!" fi linux bash bash script exit on error error-handling share|improve this question edited Oct 3 at 0:18 wjandrea 1157 asked May 6 '15 at 13:30 user3078630 9218 1 What do you want to happen on this error? How is your script called? Is it just one script or many scripts? What are the uses of your script going to look like? –Etan Reisner May 6 '15 at 13:32 just one script. I called it using ubuntu terminal like ./script/test.sh –user3078630 May 6 '15 at 13:34 unix.stackexchange.com –Seth McClaine May 6 '15 at
Bash Catch Error
13:34 @SethMcClaine it will still be an awful question there. –user4453924 May 6 '15 at 13:35 I agree but at least it would be in the right section –Seth McClaine May 6 '15 at 13:36 | show 1 more comment 1 Answer 1 active oldest votes up vote 10 down vote accepted 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. share|improve this answer edited May 6 '15 at 13:53 answered May 6 '15 at 13:33 ForceBru 10.1k31540 3 or you could send it to stderr, where errors are supposed to go. –user4453924 May 6 '15 at 13:34 how to send it to stderr ? –user3078630 May 6 '15 at 13:36 @user3078630, I've just edited my answer. 1>&2 will do the trick –ForceBru May 6 '15 at 13:37 If it's an error, you should exit with a non-zero exit status a
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and
Exit Bash Shell
policies of this site About Us Learn more about Stack Overflow the company linux exit code Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users linux exit codes Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a http://stackoverflow.com/questions/30078281/raise-error-in-bash-script minute: Sign up Error handling in BASH up vote 143 down vote favorite 110 What is your favorite method to handle errors in BASH? The best example of handling errors in BASH I have found on the web was written by William Shotts, Jr at http://www.linuxcommand.org. William Shotts, Jr suggests using the following function for error handling in BASH: #!/bin/bash # http://stackoverflow.com/questions/64786/error-handling-in-bash A slicker error handling routine # I put a variable in my scripts named PROGNAME which # holds the name of the program being run. You can get this # value from the first item on the command line ($0). # Reference: This was copied from
Bash Prompts About Writing Robust Bash Shell Scripts Many people hack together shell scripts quickly to do simple tasks, but these soon take on a life of their own. Unfortunately shell scripts are full of subtle effects which result in scripts failing in unusual ways. It's possible http://www.davidpashley.com/articles/writing-robust-shell-scripts/ to write scripts which minimise these problems. In this article, I explain several techniques for http://unix.stackexchange.com/questions/39623/trap-err-and-echoing-the-error-line writing robust bash scripts. Use set -u How often have you written a script that broke because a variable wasn't set? I know I have, many times. chroot=$1 ... rm -rf $chroot/usr/share/doc If you ran the script above and accidentally forgot to give a parameter, you would have just deleted all of your system documentation rather than making a smaller chroot. exit code So what can you do about it? Fortunately bash provides you with set -u, which will exit your script if you try to use an uninitialised variable. You can also use the slightly more readable set -o nounset. david% bash /tmp/shrink-chroot.sh $chroot= david% bash -u /tmp/shrink-chroot.sh /tmp/shrink-chroot.sh: line 3: $1: unbound variable david% Use set -e Every script you write should include set -e at the top. This tells bash that it should exit the script if any statement returns a linux exit code non-true return value. The benefit of using -e is that it prevents errors snowballing into serious issues when they could have been caught earlier. Again, for readability you may want to use set -o errexit. Using -e gives you error checking for free. If you forget to check something, bash will do it or you. Unfortunately it means you can't check $? as bash will never get to the checking code if it isn't zero. There are other constructs you could use: command if [ "$?"-ne 0]; then echo "command failed"; exit 1; fi could be replaced with command || { echo "command failed"; exit 1; } or if ! command; then echo "command failed"; exit 1; fi What if you have a command that returns non-zero or you are not interested in its return value? You can use command || true, or if you have a longer section of code, you can turn off the error checking, but I recommend you use this sparingly. set +e command1 command2 set -e On a slightly related note, by default bash takes the error status of the last item in a pipeline, which may not be what you want. For example, false | true will be considered to have succeeded. If you would like this to fail, then you can use set -o pipefail to make it fail. Program defensively - expect the unexpected Your script
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Unix & Linux Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top Trap, ERR, and echoing the error line up vote 11 down vote favorite 2 I'm trying to create some error reporting using a Trap to call a function on all errors: Trap "_func" ERR Is it possible to get what line the ERR signal was sent from? The shell is bash. If I do that, I can read and report what command was used and log/perform some actions. Or maybe I'm going at this all wrong? I tested with the following: #!/bin/bash trap "ECHO $LINENO" ERR echo hello | grep "asdf" And $LINENO is returning 2. Not working. bash shell-script error-handling trap share|improve this question edited May 29 '12 at 23:55 Gilles 370k686731123 asked May 29 '12 at 18:23 Mechaflash 4122814 You can look at the bash debugger script bashdb. It seems that the first argument to trap can contain variables that are evaluated in the desired context. So trap 'echo $LINENO' ERR' should work. –donothingsuccessfully May 29 '12 at 18:53 hmm just tried this with a bad echo | grep command and it returns the line of the Trap statement. But I'll take a look at bashdb –Mechaflash May 29 '12 at 18:56 I'm so sorry... I didn't specify in my original question that I need a native solution. I edited the question. –Mechaflash May 29 '12 at 19:05 Sorry, I borked the example line: trap 'echo $LINENO' ERR. The first argument to trap is the entire echo $LINENO hardquoted. This is in bash. –donothingsuccessfully May 29 '12 at 19:43 4 @Mechaflash It would have to be trap 'echo $LINENO' ERR, with single quotes, not double quotes. With the command you wrote, $LINENO is expanded when line 2 is parsed, so the trap is echo 2 (or rather ECHO 2, which would output bash: ECHO: command not found). –Gilles May 29 '12 at 23:56 | show 1 more comment 1 Answer 1 active oldest votes up vote 22 down vote accepted As pointed out in comments, your quoting is wrong. You need single quotes to prevent $LINENO from being expanded when the trap line is first parsed. This works: #! /bin