Bash Get Error Message
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 bash last error message us Unix & Linux Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux Stack Exchange
Bash Error Message Variable
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 bash exit with error message 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 bash get error code an automated server hardening script and one thing that they need is a 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
Bash Get Error Code From Command
question asked May 29 '14 at 7:25 Miguel Roque 1172412 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 answer edited May 29 '14 at 7:48 answered May 29 '14 at 7:42 Networker 4,69962756 I've tried that approach and it stores it DIRECTLY in the text file. I want it to store inside a string first so I can format the contents easily. –Miguel Roque May 29 '14 at 7:45 1 @MiguelRoque see updates –Networker May 29 '14 at 7:46 1 I tried putting the output inside a HEREDOC and it worked also. Thanks a lot @Networker! –Miguel Roque May 29 '14 at 7:52 add a comment| up vote 8 down vote Simply to store as a string in bash script: X=`/sbin/modprobe -n -v hfsplus 2>&1` echo $X This can be a bit better as you will see messages when command is executed: TMP=$(mktemp) /sbin/modprobe -n -v hfsplus 2>&1 | tee $TMP OUTPUT=$(cat $TMP) echo $OUTPUT rm $TMP share|improve thi
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 bash throw error developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question bash error redirect x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join
Bash Error Output
them; it only takes a minute: Sign up exit with error message in bash (oneline) up vote 11 down vote favorite 1 Is it possible to exit on error, with a message, without using if statements? [[ $TRESHOLD =~ ^[0-9]+$ http://unix.stackexchange.com/questions/132511/how-to-capture-error-message-from-executed-command ]] || exit ERRCODE "Threshold must be an integer value!" Of course the right side of || won't work, just to give you better idea of what I am trying to accomplish. Actually, I don't even mind with which ERR code it's gonna exit, just to show the message. EDIT I know this will work, but how to suppress numeric arg required showing after my custom message? [[ $TRESHOLD =~ ^[0-9]+$ ]] || exit "Threshold must be an integer http://stackoverflow.com/questions/24597818/exit-with-error-message-in-bash-oneline value!" bash message exit share|improve this question edited Jul 6 '14 at 16:23 asked Jul 6 '14 at 16:15 branquito 8891027 add a comment| 2 Answers 2 active oldest votes up vote 13 down vote accepted exit doesn't take more than one argument. To print any message like you want, you can use echo and then exit. [[ $TRESHOLD =~ ^[0-9]+$ ]] || \ { echo "Threshold must be an integer value!"; exit $ERRCODE; } share|improve this answer answered Jul 6 '14 at 16:22 P.P. 57.9k768118 The trick was using {. Great. –branquito Jul 6 '14 at 16:27 Can you explain from where it was comming the message numeric argument required, and why I couldn't disable it with 2>/dev/null? –branquito Jul 6 '14 at 16:28 Likely that TRESHOLD is empty. echo goes to stdout. So redirecting 2 will not work. You can print to stderr: [[ $TRESHOLD =~ ^[0-9]+$ ]] || { echo 1>&2 "Threshold must be an integer value!"; exit $ERRCODE; } –P.P. Jul 6 '14 at 16:30 It's not empty, because when not provided, it has default value set. And I ment on my example || exit "message", without using echo –branquito Jul 6 '14 at 16:37 1 exit accepts only an option integer (exit code). So passing a string like "message" won't work. –P.P. Jul 6 '14 at 16:44 | show 1 mo
March 20, 2016 in Debian Linux, FreeBSD, Linux, OpenBSD, RedHat/Fedora Linux, Shell scripting, Suse Linux, Ubuntu Linux, UNIXWhile writing a shell script you may need to http://www.cyberciti.biz/tips/shell-displaying-error-messages.html display an error message. For example, if you failed to open http://linuxcommand.org/wss0150.php /etc/passwd file you want to show an error message.
The old methodYou can write something as follows on bash using the if statement to test a conditioncat /etc/shadow 2>/dev/null if [ $? -ne 0 ]; then echo "Failed to open file"; exit 1 ; fiThe error message new way: OR || control operatorHowever, you can use the control operator || (or lists). It has the form as follows: command1 || command2 command2 is executed if and only if command1 returns a non-zero exit status. For example: $ cat /etc/shadow 2>/dev/null || echo "Failed to open file" This way you display an error message. bash get error Another option is to create die shell function:#!/bin/bash function die(){ echo "$1" exit 1 } # ... # ... other code cat /etc/shadow 2>/dev/null || die "Failed to open file" # rest of my scriptAND (&&) control operatorSimilarly you can use AND (&&) control operator. It has the form/syntax: command1 && command2 The command2 is executed if, and only if, command1 returns an exit status of zero. For example: $ cat /etc/shadow 2>/dev/null && echo "I can open /etc/shadow file." You can combine both to produce useful message in a script:#!/bin/bash ... tar -zcf /dev/st0 /data2 && echo "/data2/ added to backup device" || echo "Warning: Cannot add /data2/ to backup device." .... Share this on:TwitterFacebookGoogle+Download PDF version Found an error/typo on this page?About the author: Vivek Gite is a seasoned sysadmin and a trainer for the Linux/Unix & shell scripting. Follow him on Twitter. OR read more like this:How do I find the exit status of a remote command executed via ssh?BASH Sand 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, the program's ability to handle situations in which something goes wrong. Exit status 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 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 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