Bash Command 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 bash command substitution error and policies of this site About Us Learn more about Stack Overflow the bash command substitution error handling company Business Learn more about hiring developers or posting ads with us Unix & Linux Questions Tags Users Badges
Bash Error Code Of Last Command
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 takes a
Bash Error Command Not Found
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 report of all bash error message variable 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 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
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
Bash Exit With Error Message
program and a poor one is often measured in terms of the program's bash get error message robustness. That is, the program's ability to handle situations in which something goes wrong. Exit status As you recall from bash print error message 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 http://unix.stackexchange.com/questions/132511/how-to-capture-error-message-from-executed-command 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 http://linuxcommand.org/wss0150.php $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 work with the following: [me] $ true; echo $? 0 [me] $ false; echo $? 1 The true and false
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 http://stackoverflow.com/questions/962255/how-to-store-standard-error-in-a-variable-in-a-bash-script developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question http://www.cyberciti.biz/tips/shell-displaying-error-messages.html x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to store standard error in a variable in a Bash script up vote 88 down vote favorite 35 Let's say I have a script like the following: useless.sh echo "This Is error message Error" 1>&2 echo "This Is Output" And I have another shell script: alsoUseless.sh ./useless.sh | sed 's/Output/Useless/' I want to capture "This Is Error", or any other stderr from useless.sh, into a variable. Let's call it ERROR. Notice that I am using stdout for something. I want to continue using stdout, so redirecting stderr into stdout is not helpful, in this case. So, basically, I want to do ./useless.sh 2> $ERROR | ... but that obviously doesn't work. I also bash command substitution know that I could do ./useless.sh 2> /tmp/Error ERROR=`cat /tmp/Error` but that's ugly and unnecessary. Unfortunately, if no answers turn up here that's what I'm going to have to do. I'm hoping there's another way. Anyone have any better ideas? bash shell redirect variables stderr share|improve this question edited Apr 4 '12 at 7:56 l0b0 19.5k660118 asked Jun 7 '09 at 16:38 psycotica0 6951711 3 What exactly to want to use stdout for? Do you simply want to view it on the console? Or are you capturing/redirecting it's output? If it's just to the console you redirect stdout to console and stderr to stdout to capture it: ERROR=$(./useless.sh | sed 's/Output/Useless/' 2>&1 1>/dev/ttyX) –Tim Kersten Mar 23 '11 at 11:41 add a comment| 10 Answers 10 active oldest votes up vote 41 down vote accepted It would be neater to capture the error file thus: ERROR=$( outfile; } 2>&1 ) Note that the semi-colon is needed (in classic shells - Bourne, Korn - for sure; probably in Bash too). The
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 display an error message. For example, if you failed to open /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 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. 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 Shell: How to run several commands in Sequence or all at onceHowTo: Debug a Shell Script Under Linux or UNIXBash shell script tip: Run commands from a variableTest your Samba server for configuration errorsLinux / UNIX: Find Out If a Directory Exists or NotTake action or execute a command based upon shell script nameLinux/UNIX: Find Out If File Exists With Conditional ExpressionsDebian / Ubuntu Package Troubleshooting: subprocess post-removal script…Increase productivity with FTP autologin and macros{ 0 comments… add one } Security: Are you a robot or human?Please enable JavaScript to submit this form.Cancel replyLeave a Comment Name Email Comment Receive Email Notifications? no, do not subscribeyes, replies to my commentyes, all comments/replies instantlyhourly digestdaily digestweekly digest Or, you can s