Linux How to get error description by error number
The exit status of a program ($?
in the shell) is unrelated to the C errno
.
In a C program, normally the exit status comes from the argument to exit
or the return value of main
. The convention is that 0 means a successful exit (or true for the shell) and other values are a failure (ir false).
However if a program died from receiving a signal, the shell sets $?
to 128 plus the signal number. For example, on a segmentation fault (SIGSEGV
, which is 11), $?
would be 139.
To list signal numbers, I run kill -l
.
How to know what the 'errno' means?
You can use strerror()
to get a human-readable string for the error number. This is the same string printed by perror()
but it's useful if you're formatting the error message for something other than standard error output.
For example:
#include <errno.h>
#include <string.h>
/* ... */
if(read(fd, buf, 1)==-1) {
printf("Oh dear, something went wrong with read()! %s\n", strerror(errno));
}
Linux also supports the explicitly-threadsafe variant strerror_r()
.
How can I get the errors of a read operation in c
You need to look into errno
, which is a variable set by system calls to indicate an error. You can use the convenience function perror
to get a human-readable printout.
if (read(prog, value, sizeo(FRAME) == -1) {
perror("read");
// handle error
}
It can return something like No such file or directory
. Either way its a good practice to use it.
Check out man errno
and man perror
for more information.
How to differentiate between two different errors with the same error code?
There is no general way to differentiate between these conditions.
Sometimes the man page will specify conditions that you could check, but that's it.
You should design what you're building such that the differences you're trying to detect wouldn't matter. For example, if you get EAGAIN
, just try again later (or return something to the client that would cause it to try again).
How can i show error message for a particular command , if bash script terminates due to set -e
After clarification, it appears that the requirement is to exit the script if any error happens, but that the commands which are described as "badcommand" in the question, might or might not fail.
In this answer, I am naming the commands simply first_command
etc, to reflect the fact they might or might not fail.
The set -e
command, as suggested in the question, will indeed terminate the script if an error occurs, and the trap ... ERR
installs a handler which will run after an error (and before the script exits where set -e
has been used).
In this case, you should:
wait until the trap is required before installing it (it does not need to be done at/near the start of the script)
disable the trap again when it is no longer required, using
trap - ERR
so that commands to enable and disable the trap surround the command for which the trap is required.
For example:
#!/bin/bash
set -e
log_report() {
echo "Error on line $1"
}
echo "starting ..."
first_command
trap 'log_report $LINENO' ERR
echo "running"
second_command
trap - ERR
echo "still running"
third_command
This will exit if any command fails (because of the set -e
at the top), but the trap will only be run if second_command
fails.
(Note also that set -e
similarly does not need to be applied at the start of the script. It can be enabled at any point, and disabled again using set +e
. But in this example, it appears that the exit-on-error behaviour is required throughout.)
Getting error number returned by recv function
You need to include errno.h and use the errno
global variable to check the last error code. Also, you can use strerror()
to print a locale aware string explaining the error.
Example
#include <errno.h>
ssize_t size;
if ((size = recv( ... )) == -1)
{
fprintf(stderr, "recv: %s (%d)\n", strerror(errno), errno);
}
Related Topics
How to Preserve Command Line Spaces in a Linux Application
Count the Number of Executable Files in Bash
Implementing an Update/Upgrade System for Embedded Linux Devices
How to Run Vi on Docker Container
Is Usb Supported on Bash on Ubuntu on Windows 10
When Grep "\\" Xxfile I Got "Trailing Backslash"
How to Fix Symbol Lookup Error: Undefined Symbol Errors in a Cluster Environment
On Linux - Set Maximum Open Files to Unlimited. Possible
Elasticsearch Can't Write to Log Files
How to Make Find and Printf Works in Bash Script
Issue While Validating Bash Script
The Gnu Screen Is Unresponsive, Seems Blocked
How to Perform a 'Git Pull' Without Re-Entering My Ssh Password
Shell Init Issue When Click Tab, What's Wrong with Getcwd
In Linux, What Do All the Values in the "Top" Command Mean
How to Register Fuse Filesystem Type with Mount(8) and Fstab