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 shell script suppress error messages workings and policies of this site About Us Learn more about Stack Overflow
Shell Script Error Code
the company Business Learn more about hiring developers or posting ads with us Unix & Linux Questions Tags shell script error bad interpreter 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 shell script error code 1 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
Shell Script Error Exit
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|improve this
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss shell script error command not found the workings and policies of this site About Us Learn more about shell script error output Stack Overflow the company Business Learn more about hiring developers or posting ads with us Unix & Linux
Shell Script Error Too Many Arguments
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. http://unix.stackexchange.com/questions/132511/how-to-capture-error-message-from-executed-command 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 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' http://unix.stackexchange.com/questions/97101/how-to-catch-an-error-in-a-linux-bash-script NC='\e[0m' # No Color # FUNCTIONS # directoryExists - Does the directory exist? function directoryExists { cd $1 if [ $? = 0 ] then echo -e "${green}$1${NC}" else 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 371k696751126 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
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 http://stackoverflow.com/questions/25303996/how-to-capture-command-error-message-in-variable-for-if-block Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is http://www.davidpashley.com/articles/writing-robust-shell-scripts/ a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up how to capture command error message in variable for if block up vote 1 down vote favorite 1 Hi below is my code for bash shell script, in this I want to capture error message for if clause, when it says either job is already running or unable shell script to start the job to a variable, how it is possible in below script, or any other way for the below functionality if initctl start $i ; then echo "service $i started by script" else echo "not able to start service $i" fi linux bash shell share|improve this question asked Aug 14 '14 at 8:56 agarwal_achhnera 85322248 add a comment| 2 Answers 2 active oldest votes up vote 5 down vote accepted You can for example use the syntax msg=$(command 2>&1 1>/dev/null) to redirect stderr to stdout after shell script error redirecting stdout to /dev/null. This way, it will just store stderr: error=$(initctl start $i 2>&1 1>/dev/null) if [ $? -eq 0 ]; then echo "service $i started by script" else echo "service $i could not be started. Error: $error" fi This uses How to pipe stderr, and not stdout?, so that it catches stderr from initctl start $i and stores in $error variable. Then, $? contains the return code of the command, as seen in How to check if a command succeeded?. If 0, it succeeded; otherwise, some errors happened. share|improve this answer edited Aug 17 at 8:23 answered Aug 14 '14 at 9:00 fedorqui 109k30166215 Wouldn't this solution be affected by this bug ? As it is dated from 2010, I'm not sure wether it's been fixed –Aserre Aug 14 '14 at 9:05 Interesting... I wasn't aware of that. I don't have Ubuntu here with me, so we'll have to see if it works to the OP. Reading from the bug page, it doesn't seem to have been fixed. –fedorqui Aug 14 '14 at 9:29 1 Well, I have a 14.04 Ubuntu VM laying around and just tested your script. It works just fine, I was able to start cron and it returned the correct error when cron was already running. It does need sudo to run though, otherwise it returns an Unknown job: xxx error –Aserre Aug 14 '14 at 9:40 1 +1 And in case OP also wants to see stdout if successful, he could do out=$(initctl start $i 2>&1) a
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 to write scripts which minimise these problems. In this article, I explain several techniques for 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. 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 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 co