How To Print 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 shell script error handling Us Learn more about Stack Overflow the company Business Learn more about hiring
Shell Script Exit Code
developers or posting ads with us Unix & Linux Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux Stack
Bash Catch Error
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 minute: Sign up Here's how it works: Anybody can ask a
Linux Kernel Error Codes
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 the output of each command executed. I want to store the error message inside a string and bash if exit code 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 answer edited May 29 '14 at 7:48 answered May 29 '14 at 7:42 Networker 4,72962856 I've tried that approach and it stores it DIRECTLY in the text file. I want it to store inside a string fir
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 bash script exit on error Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack shell script exit on error Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Make Bash script exit bash shell exit and print error message if users invoke the script incorrectly up vote 3 down vote favorite 1 Script needed was #!/bin/bash # Check if there are two arguments if [ $# -eq 2 ]; then # Check if the input file http://unix.stackexchange.com/questions/132511/how-to-capture-error-message-from-executed-command actually exists. if ! [[ -f "$1" ]]; then echo "The input file $1 does not exist." exit 1 fi else echo "Usage: $0 [inputfile] [outputfile]" exit 1 fi # Run the command on the input file grep -P "^[\s]*[0-9A-Za-z-]+.?[\s]*$" "$1" > "$2" Edit, the script has changed to grep -P "^[\s]*[0-9A-Za-z-]+.?[\s]*$" $* if [ ! -f "$1" ]; then echo 'Usage: ' echo echo './Scriptname inputfile > outputfile' exit 0 fi invoking the script with no parameters gives no erros and sits blank Usage: ./Scriptname http://stackoverflow.com/questions/12966622/make-bash-script-exit-and-print-error-message-if-users-invoke-the-script-incorre inputfile > outputfile I have bit of code grep -P "^[\s]*[0-9A-Za-z-]+.?[\s]*$" $* This code pulls lines that have a single word on them and pumps the output to a new file, so for example This is a multi word line this the above line is not now once again wrong The output would be This now The code works, users invoke the code using ./scriptname file > newfile However, I am trying to expand the code to give users an error message if they invoke the script incorrectly. For the error messange, I'm thinking of echoing something back like scriptname file_to_process > output_file. I did try if [incorrectly invoted unsure what to type] echo $usage exit 1 Usage="usage [inputfile] [>] [outputfile] However I have had little luck. The code runs but does nothing if I invoke with just the script name. Also, if I invoke the script with just the scriptname and the input file, it will output the results instead of exiting with the error message. Other ones I have tried are if [ ! -n $1 ]; then echo 'Usage: ' echo echo './Scriptname inputfile > outputfile' exit 0 fi Given replies I have received so far, my code now is #!/bin/bash grep -P "^[\s]*[0-9A-Za-z-]+.?[\s]*$" $* if [ ! -f "$1" ]; then echo 'Usage: ' echo echo './Scriptname inputfile > outputfile' exit 0 fi When invoking the script without an input file the script does nothing and has to be aborted with ctrl+c, still trying to get the echo of the invoke mess
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 screen. How http://www.cyberciti.biz/faq/linux-redirect-error-output-to-file/ do I store and redirect output from the computer screen to a 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 (stderr) as follows:command-name shell script 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 discarded by your system. This shell script exit 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 comments… add one } Andrey Oliveira June 23, 2014, 12:15 pmIn new