How To Get Error Code In Unix
Contents |
Ramey
The exit command terminates a script, just as in a C program. It can also return a value, which is available to the unix exit codes script's parent process.Every command returns an exit status (sometimes referred exit code 0 to as a return status or exit code). A successful command returns a 0, whileBash Script Exit On Error
an unsuccessful one returns a non-zero value that usually can be interpreted as an error code. Well-behaved UNIX commands, programs, and utilities return a 0 exit code upon successful completion, though there
Exit Code Python
are some exceptions.
Likewise, functions within a script and the script itself return an exit status. The last command executed in the function or script determines the exit status. Within a script, an exit nnn command may be used to deliver an nnn exit status to the shell (nnn must be an integer in the 0 exit code 1 - 255 range).When a script ends with an exit that has no parameter, the exit status of the script is the exit status of the last command executed in the script (previous to the exit).#!/bin/bash COMMAND_1 . . . COMMAND_LAST # Will exit with status of last command. exitThe equivalent of a bare exit is exit $? or even just omitting the exit.#!/bin/bash COMMAND_1 . . . COMMAND_LAST # Will exit with status of last command. exit $?#!/bin/bash COMMAND1 . . . COMMAND_LAST # Will exit with status of last command.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
Bash Return Value From Function
and commands under Linux / UNIX operating system? A. All UNIX and Linux bash exit on error command has a several parameters or variables that can be use to find out the exit status of command. exit command in unix shell script 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 http://tldp.org/LDP/abs/html/exit-status.html expands to the status 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 http://www.cyberciti.biz/faq/shell-how-to-determine-the-exit-status-of-linux-and-unix-command/ Output:bash: cyberciti: command not foundDisplay exit status 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
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 http://unix.stackexchange.com/questions/9857/how-can-i-get-the-error-code-exit-code-of-xdg-mime-query-filetype-command Stack Overflow the company Business Learn more about hiring developers or posting ads with us Unix & Linux Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux Stack Exchange is a question and answer http://stackoverflow.com/questions/8211844/get-exit-code-for-command-in-bash-ksh 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 can answer The best answers are exit code voted up and rise to the top How can I get the error code (exit code) of “xdg-mime query filetype” command? up vote 0 down vote favorite I ran xdg-mime query filetype
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 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Get exit code for command in bash/ksh up vote 34 down vote favorite 5 I want to write code like this: command="some command" safeRunCommand $command safeRunCommand() { cmnd=$1 $($cmnd) if [ $? != 0 ]; then printf "Error when executing command: '$command'" exit $ERROR_CODE fi } But this code does not working the way I want. Where I made mistake? bash unix ksh exit share|improve this question edited Dec 30 '13 at 19:34 javaPlease42 1,1981233 asked Nov 21 '11 at 12:32 Kolesar 3291925 add a comment| 4 Answers 4 active oldest votes up vote 44 down vote accepted Below is the fixed code: #!/bin/ksh safeRunCommand() { typeset cmnd="$*" typeset ret_code echo cmnd=$cmnd eval $cmnd ret_code=$? if [ $ret_code != 0 ]; then printf "Error : [%d] when executing command: '$cmnd'" $ret_code exit $ret_code fi } command="ls -l | grep p" safeRunCommand "$command" Now if you look into this code few things that I changed are: use of typeset is not necessary but a good practice. It make cmnd and ret_code local to safeRunCommand use of ret_code is not necessary but a good practice to store return code in some variable (and store it ASAP) so that you can use it later like I did in printf "Error : [%d] when executing command: '$command'" $ret_code pass the command with quotes surrounding the command like safeRunCommand "$command". If you dont then cmnd will get only the value ls and not ls -l. And it is even more important if your command contains pipes. you can use typeset cmnd="$*" instead of typeset cmnd="$1" if you want to keep the spaces. You can try with both depending upon how complex is your command argument. eval is used to evaluate so that command containing pipes can work fine NOTE: Do remember some commands give 1 as return code even though there is no error like grep. If grep found something it will return 0 else 1. I had tested with KSH/BASH. And it worked fine. Let me know if u face issues running this. share|