Linux Error Commands
Contents |
and Signals and Traps (Oh My!) - Part 1 by William Shotts, Jr. In this lesson, we're going to look at handling errors during the execution of your scripts. The difference between a good program and a poor one is often measured in terms of the program's robustness. That is, shell script error handling the program's ability to handle situations in which something goes wrong. Exit status As you recall from shell script exit code previous lessons, every well-written program returns an exit status when it finishes. If a program finishes successfully, the exit status will be zero. If the bash catch error exit status is anything other than zero, then the program failed in some way. It is very important to check the exit status of programs you call in your scripts. It is also important that your scripts return a meaningful exit linux kernel error codes status when they finish. I once had a Unix system administrator who wrote a script for a production system containing the following 2 lines of code: # Example of a really bad idea cd $some_directory rm * Why is this such a bad way of doing it? It's not, if nothing goes wrong. The two lines change the working directory to the name contained in $some_directory and delete the files in that directory. That's the intended behavior. But what happens if the
Bash If Exit Code
directory named in $some_directory doesn't exist? In that case, the cd command will fail and the script executes the rm command on the current working directory. Not the intended behavior! By the way, my hapless system administrator's script suffered this very failure and it destroyed a large portion of an important production system. Don't let this happen to you! The problem with the script was that it did not check the exit status of the cd command before proceeding with the rm command. Checking the exit status There are several ways you can get and respond to the exit status of a program. First, you can examine the contents of the $? environment variable. $? will contain the exit status of the last command executed. You can see this work with the following: [me] $ true; echo $? 0 [me] $ false; echo $? 1 The true and false commands are programs that do nothing except return an exit status of zero and one, respectively. Using them, we can see how the $? environment variable contains the exit status of the previous program. So to check the exit status, we could write the script this way: # Check the exit status cd $some_directory if [ "$?" = "0" ]; then rm * else echo "Cannot change directory!" 1>&2 exit 1 fi In this version, we examine the exit status of the cd command and if it's not zero, we print an error message on standard err
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the
Bash Script Exit On Error
workings and policies of this site About Us Learn more about Stack shell script exit on error Overflow the company Business Learn more about hiring developers or posting ads with us Unix & Linux Questions Tags bash if exit code not 0 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 http://linuxcommand.org/wss0150.php 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 How to capture error message from executed command? up vote 5 down vote favorite 1 I was tasked to create an automated server hardening script and one thing that they need is a http://unix.stackexchange.com/questions/132511/how-to-capture-error-message-from-executed-command report of all the output of each command executed. I want to store the error message inside a string and append it in a text file. Let's say I ran this command: /sbin/modprobe -n -v hfsplus The output of running this in my machine would be: FATAL: Module hfsplus not found How can I store that error message inside a string? Any help would be greatly appreciated. Thanks! bash scripting string share|improve this question asked May 29 '14 at 7:25 Miguel Roque 1173412 I tried running this command: var=$(/sbin/modprobe -n -v hfsplush) And then displaying it: $var But it still doesn't capture the error message inside the string. –Miguel Roque May 29 '14 at 7:42 add a comment| 4 Answers 4 active oldest votes up vote 10 down vote you can do it by redirecting errors command: /sbin/modprobe -n -v hfsplus 2> fileName as a script #!/bin/bash errormessage=$( /sbin/modprobe -n -v hfsplus 2> &1) echo $errormessage or #!/bin/bash errormessage=`/sbin/modprobe -n -v hfsplus 2> &1 ` echo $errormessage if you want to append the error use >> instead of > share|improv
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the http://unix.stackexchange.com/questions/9857/how-can-i-get-the-error-code-exit-code-of-xdg-mime-query-filetype-command 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 http://askubuntu.com/questions/29370/how-to-check-if-a-command-succeeded 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 shell script 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 How can I get the error code (exit code) of “xdg-mime query filetype” command? up vote 0 down vote favorite I ran xdg-mime query filetype
communities company blog Stack Exchange Inbox Reputation and Badges sign up log in tour help Tour Start 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 Ask Ubuntu Questions Tags Users Badges Unanswered Ask Question _ Ask Ubuntu is a question and answer site for Ubuntu users and developers. 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 How to check if a command succeeded? up vote 63 down vote favorite 22 Is there any way to check if there is an error in executing a command? Example : test1=`sed -i "/:@/c connection.url=jdbc:oracle:thin:@$ip:1521:$dataBase" $search` valid $test1 function valid () { if $test -eq 1; then echo "OK" else echo "ERROR" fi } I already tried do that but it seems it isn't working. I don't how do that. command-line share|improve this question edited Mar 7 '11 at 13:41 Octavian Damiean 10.2k54260 asked Mar 7 '11 at 11:40 moata_u 72861523 5 Prefer $(foo) over backticks `foo`, because you can nest it, and it's easier to distinguish from apostrophes. –user unknown Mar 26 '11 at 18:19 add a comment| 5 Answers 5 active oldest votes up vote 100 down vote accepted The return value is stored in $?. 0 indicates success, others indicates error. some_command if [ $? -eq 0 ]; then echo OK else echo FAIL fi Like any other textual value, you can store it in a variable for future comparison: some_command retval=$? do_something $retval if [ $retval -ne 0 ]; then echo "Return code was not zero but $retval" fi For possible comparison operators, see man test. share|improve this answer edited Mar 7 '11 at 13:14 answered Mar 7 '11 at 12:06 Lekensteyn 86.3k35220293 That's nice ...can i hold the output error ??!! , because in this case i have 2 error : command error "text" ex, file not found and my error "text" Which is in this case failed for example –moata_u Mar 7 '11 at 12:28 @moata_u: you can store the value in a variable as shown in my answer. –Lekensteyn Mar 7 '11 at 13:14 @moata_u: You need to put a ; before the els