Linux Command Error Messsage
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 shell script error handling about hiring developers or posting ads with us Unix & Linux Questions Tags Users Badges Unanswered
Shell Script Exit Code
Ask Question _ Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. bash catch error 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 capture error message from executed shell script exit on error 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 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
Bash Error Message Variable
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 answer edited May 29 '14 at 7:48 answered May 29 '14 at 7:42 Networker 4,74462856 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/m
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 linux kernel error codes Overflow the company Business Learn more about hiring developers or posting ads with us bash script exit on error Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a
"1>&2"
community of 6.2 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 http://unix.stackexchange.com/questions/132511/how-to-capture-error-message-from-executed-command 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 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 http://stackoverflow.com/questions/25303996/how-to-capture-command-error-message-in-variable-for-if-block 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 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 110k30167217 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.
am a new Ubuntu Linux and bash shell user. I also know how to redirect output from display/screen to a file using the following syntax:
cmd > file ls > fileHowever, some time errors are displayed on http://www.cyberciti.biz/faq/linux-redirect-error-output-to-file/ screen. How do I store and redirect output from the computer screen to a http://www.unix.com/shell-programming-and-scripting/108220-how-capture-actual-error-message-when-command-fails-execute.html file on a Linux or Unix-like systems? Bash / ksh and other modern shell on Linux has three file descriptors:stdin (0)stdout (1)stderr (2)Syntax To redirect all output to fileThe syntax is as follows to redirect output (stdout) as follows:command-name > output.txt command-name > stdout.txtSyntax To redirect all error to fileThe syntax is as follows to redirect errors shell script (stderr) as follows:command-name 2> errors.txt command-name 2> stderr.txtSyntax to redirect both output (stdout) and errors (stderr) to different filesThe syntax:command1 > out.txt 2> err.txt command2 -f -z -y > out.txt 2> err.txtSyntax to redirect both output (stdout) and errors (stderr) to same fileThe syntax is:command1 > everything.txt 2>&1 command1 -arg > everything.txt 2>&1Syntax to redirect errors (stderr) to null or zero devicesData written to a null or zero special file is shell script exit discarded by your system. This is useful to silence out errors (also know as ‘error spam'):command1 2> /dev/null command1 2> /dev/zero command2 -arg 2> /dev/null command2 -arg 2> /dev/zeroTip: Use tee command to redirect to both a file and the screen same timeThe syntax is:command1 |& tee log.txt ## or ## command1 -arg |& tee log.txt ## or ## command1 2>&1 | tee log.txtAnother usage:#!/bin/bash # My script to do blah ... foo(){ : } 2>&1 | tee foo.logOR#!/bin/bash # My script to do blah ... { command1 command2 } 2>&1 | tee script.log Share this tutorial 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:BASH Shell Redirect Output and Errors To /dev/nullBASH Shell: How To Redirect stderr To stdout ( redirect stderr to a File )Unix and Linux: Redirect Error Output To null CommandHow do I save or redirect stdout and stderr into different files?Linux / Unix Find Command: Avoid Permission Denied MessagesShell Script While Loop ExamplesPrinting output of c program to a file in LinuxUnix / Linux: Save Output To FileLinux iptables: Port Redirection ExampleApplescript: Run or Call a Shell Script{ 10 cScripting 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 Page... linux operating commands and unix 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 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 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