How To Capture Error Message In Shell Script
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
Shell Script Error Handling
Learn more about Stack Overflow the company Business Learn more about hiring developers or shell script exit code posting ads with us Unix & Linux Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux Stack Exchange is bash error message variable 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
Bash Catch Error
can answer The best answers are voted up and rise to the top How to catch an error in a linux bash script? up vote 5 down vote favorite 1 I made the following script: # !/bin/bash # OUTPUT-COLORING red='\e[0;31m' green='\e[0;32m' NC='\e[0m' # No Color # FUNCTIONS # directoryExists - Does the directory exist? function directoryExists { cd $1 if [ $? = 0 ] then echo -e "${green}$1${NC}" else
Shell Script Exit On Error
echo -e "${red}$1${NC}" fi } # EXE directoryExists "~/foobar" directoryExists "/www/html/drupal" The script works, but beside my echoes, there is also the output when cd $1 fails on execution. testscripts//test_labo3: line 11: cd: ~/foobar: No such file or directory Is it possible to catch this? bash shell shell-script error-handling share|improve this question edited Oct 22 '13 at 22:58 Gilles 372k696751126 asked Oct 22 '13 at 10:29 Thomas De Wilde 28114 Just an FYI, you can also do this a lot simpler; test -d /path/to/directory ( or [[ -d /path/to/directory ]] in bash ) will tell you whether a given target is a directory or not, and it will do it quietly. –Patrick Oct 22 '13 at 12:36 @Patrick, that just tests if it's a directory, not if you can cd into it. –Stéphane Chazelas Oct 22 '13 at 12:54 @StephaneChazelas yes. The function name is directoryExists. –Patrick Oct 22 '13 at 13:57 add a comment| 5 Answers 5 active oldest votes up vote 4 down vote accepted Your script changes directories as it runs, which means it won't work with a series of relative pathnames. You then commented later that you only wanted to check for directory existence, not t
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 bash catch error message between a good program and a poor one is often measured in terms
Bash Script Exit On Error
of the program's robustness. That is, the program's ability to handle situations in which something goes wrong. Exit status "1>&2" As you recall from 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 exit status http://unix.stackexchange.com/questions/97101/how-to-catch-an-error-in-a-linux-bash-script 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 status when they finish. I once had a Unix system administrator who wrote a script for a production system containing the following 2 lines http://linuxcommand.org/wss0150.php 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 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 wo
Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here. Search Forums Show Threads Show Posts Tag Search Advanced Search Unanswered Threads Find All Thanked Posts Go to http://www.unix.com/shell-programming-and-scripting/108220-how-capture-actual-error-message-when-command-fails-execute.html Page... unix and linux operating commands How to capture actual error message when a command fails to execute Shell Programming and Scripting Thread Tools Search this Thread Display Modes #1 04-26-2009 prathima Registered User Join Date: Apr 2008 Last Activity: 26 April 2009, 12:49 PM EDT Posts: 5 Thanks: 0 Thanked 0 Times in 0 Posts How to capture actual error message when a command shell script fails to execute I want to capture actual error message in case the commands I use in my shell script fails. For eg: ls -l abc.txt 2>>errorlog.txt In this case I understand the error message is written to the errorlog.txt and I assume its bacause the return code from the command ls -l abc might return 2 if "abc" doesnt exists. My question is: How about if the command return non zero return code and is not shell script exit equal to 2? I want to do something like this: ls -l abc.txt if [$? -ne 0] then echo $errorMessage >> errorlog.txt fi where I want actual error message that the command has returned to be written to errorlog.txt. Just for understanding I used $errorMessage but I assume there should be some means to capture the actual error message which can later be stored in errorMessage or written directly to errorlog.txt. Can someone please help? Remove advertisements Sponsored Links prathima View Public Profile Find all posts by prathima #2 04-26-2009 devtakh Unix Enthusiatic Join Date: Oct 2007 Last Activity: 21 August 2013, 5:20 AM EDT Location: Bangalore Posts: 738 Thanks: 0 Thanked 7 Times in 7 Posts Quote: Originally Posted by prathima I want to capture actual error message in case the commands I use in my shell script fails. For eg: ls -l abc.txt 2>>errorlog.txt In this case I understand the error message is written to the errorlog.txt and I assume its bacause the return code from the command ls -l abc might return 2 if "abc" doesnt exists. Quote: Here, 2 is not the return code. 2 is the File descriptor value(FD). By default, the standard inputs FD is 0, std o/p is 1 and std error is 2. That is the reason why your std errors are getting redirected to the err