Linux exit status error

How do I get the list of exit codes (and/or return codes) and meaning for a command/utility?

Is there a way I can do what stated in the title from the terminal commands, or will I have to look into the codes?

4 Answers 4

Exit codes indicates a failure condition when ending a program and they fall between 0 and 255. The shell and its builtins may use especially the values above 125 to indicate specific failure modes, so list of codes can vary between shells and operating systems (e.g. Bash uses the value 128+N as the exit status). See: Bash — 3.7.5 Exit Status or man bash .

In general a zero exit status indicates that a command succeeded, a non-zero exit status indicates failure.

To check which error code is returned by the command, you can print $? for the last exit code or $ which gives a list of exit status values from pipeline (in Bash) after a shell script exits.

There is no full list of all exit codes which can be found, however there has been an attempt to systematize exit status numbers in kernel source, but this is main intended for C/C++ programmers and similar standard for scripting might be appropriate.

Some list of sysexits on both Linux and BSD/OS X with preferable exit codes for programs (64-78) can be found in /usr/include/sysexits.h (or: man sysexits on BSD):

0 /* successful termination */ 64 /* base value for error messages */ 64 /* command line usage error */ 65 /* data format error */ 66 /* cannot open input */ 67 /* addressee unknown */ 68 /* host name unknown */ 69 /* service unavailable */ 70 /* internal software error */ 71 /* system error (e.g., can't fork) */ 72 /* critical OS file missing */ 73 /* can't create (user) output file */ 74 /* input/output error */ 75 /* temp failure; user is invited to retry */ 76 /* remote error in protocol */ 77 /* permission denied */ 78 /* configuration error */ /* maximum listed value */ 

The above list allocates previously unused exit codes from 64-78. The range of unallotted exit codes will be further restricted in the future.

Читайте также:  Linux ubuntu server ftp server

However above values are mainly used in sendmail and used by pretty much nobody else, so they aren’t anything remotely close to a standard (as pointed by @Gilles).

In shell the exit status are as follow (based on Bash):

  • 1 — 125 — Command did not complete successfully. Check the command’s man page for the meaning of the status, few examples below:
  • 1 — Catchall for general errors
$ let "var1 = 1/0"; echo $? -bash: let: var1 = 1/0: division by 0 (error token is "0") 1 

Missing keyword or command, or permission problem (and diff return code on a failed binary file comparison).

$ curl foo; echo $? curl: (6) Could not resolve host: foo 6 
$ /dev/null $ /etc/hosts; echo $? -bash: /etc/hosts: Permission denied 126 
$ foo; echo $? -bash: foo: command not found 127 
$ exit 3.14159 -bash: exit: 3.14159: numeric argument required 
$ hexdump -n100000 /dev/urandom | tee &>/dev/null >(cat > file1.txt) >(cat > file2.txt) >(cat > file3.txt) >(cat > file4.txt) >(cat > file5.txt) $ find . -name '*.txt' -print0 | xargs -r0 cat | tee &>/dev/null >(head /dev/stdin > head.out) >(tail /dev/stdin > tail.out) xargs: cat: terminated by signal 13 $ echo $ 0 125 141 
$ sleep 5 && killall sleep & [1] 19891 $ sleep 100; echo $? Terminated: 15 143 
$ sh -c 'exit 3.14159'; echo $? sh: line 0: exit: 3.14159: numeric argument required 255 

According to the above table, exit codes 1 — 2, 126 — 165, and 255 have special meanings, and should therefore be avoided for user-specified exit parameters.

Please note that out of range exit values can result in unexpected exit codes (e.g. exit 3809 gives an exit code of 225, 3809 % 256 = 225).

Источник

Bash exit on error

An exit status code is returned when any Linux command is executed from the terminal, either the command is successful or unsuccessful. This status code can be used to show the error message for unsuccessful execution or perform any particular task by using shell script. The exit status code always represents by a number. The value of this code is 0 for the successful execution of any Linux command and it returns any number from 1 to 255 for the unsuccessful execution of the command. How the exist status code can be used from the terminal and in the bash script are shown in this tutorial.

Some common error status codes are mention below.

Code Description Comments
0 It indicates successful execution.
1 It is used to catch all general errors. “Divide by zero”, “Operation not permitted” etc. can be the error messages of this code.
2 It indicates the abuse of shell built-ins. “Missing keyword”, “No such file or directory” etc. can be the error messages of this code.
126 It generates when the any command is unable to execute. Permission problem or required key not available can generate this status code
127 It normally generates for the command path problem. “Command not found” can be the message for this error code.
130 It generates for fatal error. “Script terminated by Ctrl+C” can be the message of this code.
255* It indicates exit code out of range.

Example-1: Reading exit code from the terminal

‘$?’ shell variable can be used to display the exit code of any command. ‘ls –la’ is a valid command and it shows the list of files and folders of the current working directory. The value of ‘$?’ will be 0 after executing ‘ls -la’ command. ‘ls –xyz’ is an invalid command and ‘$?’ will return 2 as error code after executing the command.

Example-2: Reading exit code in bash script

Create a bash file named read_file.sh with the following script. In this script, the file name will be taken as user’s input and, total number of lines, words and characters of that file will be counted by using `wc` command. If the file name is valid then the value of $status_code is 0 and if the file name is invalid, then then the value of $status_code is 1.

read_file.sh

#!/bin/bash
echo «Enter the filename»
read filename
wc -lwc $filename
status_code = $?
echo «The exit of ‘wc’ command is : $status_code «

Example-3: Using exit code value for doing specific task

Create a bash file named read_month.sh with the following code. Here, a date value will be taken as input. The name of the month will retrieve from the date value if the input date is valid otherwise “invalid date” error message will appear. ‘if’ condition is used in the script to check the exit status code of the date command. If the condition is true, then the success message and month name of the date will be printed. If the condition is false, then the failure message and exit status code, 1 will print.

read_month.sh

#!/bin/bash
echo «Enter a date in the format: YYYY-MM-DD»
read date_value
current_month =$ ( date -d » $date_value » ‘+%B’ )
if [ $? -eq 0 ]
then
echo «Date command is executed Successfully»
echo «Current month is $current_month »
else
echo «Date command is not executed Successfully»
exit 1
fi

Example-4: Using && and || with exit code

‘&&’ Logical operator is used for successful exit code and ‘||’ logical operator is used for unsuccessful exit code. The following command will print ‘File exists’ if book.txt file exists in the current location and print ‘File not exists’ if book.txt file not exists in the current location.

Conclusion:

Different uses of exit status code are shown in this tutorial. Hope, the reader will get a clear concept about exit status code of bash after reading this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

Источник

Оцените статью
Adblock
detector