Linux Bash Error Status
Contents |
>ExampleComments1Catchall for general errorslet "var1 = 1/0"Miscellaneous errors, such as "divide by zero" and other impermissible bash if exit code operations2Misuse of shell builtins (according to Bash documentation)empty_function() {}Missing keyword or command, or permission problem (and diff return code on a
Bash Script Exit On Error
failed binary file comparison).126Command invoked cannot execute/dev/nullPermission problem or command is not an executable127"command not found"illegal_commandPossible
Linux Exit Code
problem with $PATH or a typo128Invalid argument to exitexit 3.14159exit takes only integer args in the range 0 - 255 (see first footnote)128+nFatal error signal "n"kill -9 $PPID of script$? returns 137 (128 + 9)130Script terminated by Control-CCtl-CControl-C is fatal error signal 2, (130 = 128 + 2, see above)255*Exit status out of rangeexit -1exit takes only integer args in the range 0 - 255
exit codes, exit codes are important and this article describes how to use them in your scripts and understand them in general. Written by Benjamin Cane on 2014-09-02 14:45:00| 4 min read Sponsored by Lately I've been working on a
Bash Return Value From Function
lot of automation and monitoring projects, a big part of these projects are taking existing scripts bash if exit code not 0 and modifying them to be useful for automation and monitoring tools. One thing I have noticed is sometimes scripts use exit codes and sometimes they bash not equal don't. It seems like exit codes are easy for poeple to forget, but they are an incredibly important part of any script. Especially if that script is used for the command line. What are exit codes? On Unix and Linux http://www.tldp.org/LDP/abs/html/exitcodes.html systems, programs can pass a value to their parent process while terminating. This value is referred to as an exit code or exit status. On POSIX systems the standard convention is for the program to pass 0 for successful executions and 1 or higher for failed executions. Why is this important? If you look at exit codes in the context of scripts written to be used for the command line the answer is very simple. Any script that is useful in http://bencane.com/2014/09/02/understanding-exit-codes-and-how-to-use-them-in-bash-scripts/ some fashion will inevitably be either used in another script, or wrapped with a bash one liner. This becomes especially true if the script is used with automation tools like SaltStack or monitoring tools like Nagios, these programs will execute scripts and check the status code to determine whether that script was successful or not. On top of those reasons, exit codes exist within your scripts even if you don't define them. By not defining proper exit codes you could be falsely reporting successful executions which can cause issues depending on what the script does. What happens if I don't specify an exit code In Linux any script run from the command line has an exit code. With Bash scripts, if the exit code is not specified in the script itself the exit code used will be the exit code of the last command run. To help explain exit codes a little better we are going to use a quick sample script. Sample Script: #!/bin/bash touch /root/test echo created file The above sample script will execute both the touch command and the echo command. When we execute this script (as a non-root user) the touch command will fail, ideally since the touch command failed we would want the exit code of the script to indicate failure with an appropriate exit code. To check the exit code we can simply print the $? special variable in bash. This variable will print the
1, 2008 in AIX, BASH Shell, CentOS, Debian / Ubuntu, FreeBSD, Linux, Openbsd, Programming, RedHat and Friends, Solaris-Unix, Ubuntu Linux, UNIXQ. Can you explain the exit status of shell and commands under http://www.cyberciti.biz/faq/shell-how-to-determine-the-exit-status-of-linux-and-unix-command/ Linux / UNIX operating system? A. All UNIX and Linux command has a http://stackoverflow.com/questions/1221833/bash-pipe-output-and-capture-exit-status several parameters or variables that can be use to find out the exit status of command. Please note that these parameters or variables may only be referenced assignment to them is not allowed. You can use $? to find out the exit status of command. $? always expands to the status exit code of the most recently executed foreground command or pipeline. For example, you run the command cal: $ cal Now to see exit status of cal command type following command: $ echo $? Output:
0Zero means command executed successfully, if exit status returns non-zero value then your command failed to execute. For example run command called cyberciti $ cyberciti Output:bash: cyberciti: command not foundDisplay exit status bash if exit of the command: $ echo $? Output:127Value 127 (non-zero) indicates command cyberciti failed to execute. You can use exit status in shell scripting too. You can store result of exit status in variable. Consider following shell script:#!/bin/bash echo -n "Enter user name : " read USR cut -d: -f1 /etc/passwd | grep "$USR" > /dev/null OUT=$? if [ $OUT -eq 0 ];then echo "User account found!" else echo "User account does not exists in /etc/passwd file!" fiSave and execute the script as follows: $ chmod +x script.sh$ ./script.sh Output:Enter user name : jradmin User account does not exists in /etc/passwd fileTry it one more time: $ ./script.sh Output:Enter user name : vivek User account foundAs you can see, I have used grep command to find out user name stored in USR variable. If grep command finds user name in /etc/passwd command output it would return exit status of zero. This is stored in OUT variable. Next, if command makes decision based upon exit status stored in OUT variable. 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 t
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 Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up bash: pipe output AND capture exit status up vote 219 down vote favorite 57 I want to execute a long running command in bash shell, and both capture its exit status, and tee its output. So I do this command | tee out.txt ST=$? The problem is that the variable ST captures the exit status of tee and not of command. How can I solve this? Note that command is long running and redirecting the output to a file to view it later is not a good solution for me. linux bash shell redirect tee share|improve this question edited May 17 '13 at 15:17 lesmana 13k64470 asked Aug 3 '09 at 11:31 flybywire 64.9k146335457 [[ "${PIPESTATUS[@]}" =~ [^0\ ] ]] && echo -e "Match - error found" || echo -e "No match - all good" This will test all the values of the array at once and give an error message if any of the pipe values returned are not zero. This is a pretty robust generalized solution for detecting errors in a piped situation. –Brian S. Wilson Mar 31 '15 at 19:08 add a comment| 13 Answers 13 active oldest votes up vote 297 down vote accepted There is an internal Bash variable called $PIPESTATUS; it’s an array that holds the exit status of each command in your last foreground pipeline of commands.