Display 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 hide error message Learn more about Stack Overflow the company Business Learn more about hiring developers or
Shell Script Suppress Error Messages
posting ads with us Unix & Linux Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux Stack Exchange is shell script error code 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
Shell Script Error Bad Interpreter
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 the output of each command executed. I want to store the error message inside a string and append it in shell script error code 1 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 answer edited May 29 '14 at 7:48 answered May 29 '14 at 7:42 Networker 4,70962756 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 easil
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss
Shell Script Error Exit
the workings and policies of this site About Us Learn more about
Shell Script Error Command Not Found
Stack Overflow the company Business Learn more about hiring developers or posting ads with us Unix & Linux Questions shell script error output 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. Join them; http://unix.stackexchange.com/questions/132511/how-to-capture-error-message-from-executed-command 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' NC='\e[0m' # http://unix.stackexchange.com/questions/97101/how-to-catch-an-error-in-a-linux-bash-script 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 370k686731123 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
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings http://stackoverflow.com/questions/64786/error-handling-in-bash 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 Questions Jobs Documentation Tags Users Badges Ask Question 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 shell script takes a minute: Sign up Error handling in BASH up vote 143 down vote favorite 110 What is your favorite method to handle errors in BASH? The best example of handling errors in BASH I have found on the web was written by William Shotts, Jr at http://www.linuxcommand.org. William Shotts, Jr suggests using the following function for error handling in shell script error BASH: #!/bin/bash # A slicker error handling routine # I put a variable in my scripts named PROGNAME which # holds the name of the program being run. You can get this # value from the first item on the command line ($0). # Reference: This was copied from