Meaning of Exit Status 1 Returned by Linux Command

Exit status -1 from a Linux shell script

Have you ever tried to read the documentation?

Returns:

the exitstatus returned by the remote command, or -1, if the command not yet terminated (or this channel type has no command).

bash read returns with exit code 1 even though it runs as expected

read is returning 1 because it returns 0 only when end-of-file is not encountered.

As per help read:

Exit Status:
The return code is zero, unless end-of-file is encountered

You don't even need a read here, just use $(<file) to read the file content into a variable:

echo "yyy" > xxx
payload=$(<xxx)
echo $?
echo "$payload"

It is advisable to not use all uppercase variable names in order to avoid clash with ENV variables.

find and delete command in linux returning exit status 1 after deleting files

The man find says

find exits with status 0 if all files are processed successfully,
greater than 0 if errors occur. This is deliberately a very broad
description, but if the return value is non-zero, you should not rely
on the correctness of the results of find.

That means if at least one directory was not possible to delete then the find returns something else than 1. If there is a directory structure like

/some/thing/subdir
/some/thing/subdir/subsubdir

and both subsdirs are eligible to delete then find may delete the /some/thing/subdir first and then delete of the /some/thing/subdir/subsubdir fails because it does not exist anymore.


EDIT

The original answer contains explanation why but does not contain a proposal how to solve it. Deleep's comment proposed to use the -depth parameter to force find process each directory's contents before the directory itself. Indeed it works:

find /some/thing/ -type d -mtime +7 -depth -exec rm -rf {} \;

Are there any standard exit status codes in Linux?

8 bits of the return code and 8 bits of the number of the killing signal are mixed into a single value on the return from wait(2) & co..

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>

int main() {
int status;

pid_t child = fork();
if (child <= 0)
exit(42);
waitpid(child, &status, 0);
if (WIFEXITED(status))
printf("first child exited with %u\n", WEXITSTATUS(status));
/* prints: "first child exited with 42" */

child = fork();
if (child <= 0)
kill(getpid(), SIGSEGV);
waitpid(child, &status, 0);
if (WIFSIGNALED(status))
printf("second child died with %u\n", WTERMSIG(status));
/* prints: "second child died with 11" */
}

How are you determining the exit status? Traditionally, the shell only stores an 8-bit return code, but sets the high bit if the process was abnormally terminated.


$ sh -c 'exit 42'; echo $?
42
$ sh -c 'kill -SEGV $$'; echo $?
Segmentation fault
139
$ expr 139 - 128
11

If you're seeing anything other than this, then the program probably has a SIGSEGV signal handler which then calls exit normally, so it isn't actually getting killed by the signal. (Programs can chose to handle any signals aside from SIGKILL and SIGSTOP.)

What is the difference between exit(0) and exit(1) in C?

What is the difference between exit(0) and exit(1) in C language?

exit(0) indicates successful program termination & it is fully portable, While

exit(1) (usually) indicates unsucessful termination. However, it's usage is non-portable.

Note that the C standard defines EXIT_SUCCESS and EXIT_FAILURE to return termination status from a C program.

0 and EXIT_SUCCESS are the values specified by the standard to indicate successful termination, however, only EXIT_FAILURE is the standard value for returning unsucessful termination. 1 is used for the same in many implementations though.


Reference:

C99 Standard: 7.20.4.3 The exit function
Para 5

Finally, control is returned to the host environment. If the value of status is zero or
EXIT_SUCCESS, an implementation-defined form of the status successful termination is
returned. If the value of status is EXIT_FAILURE , an implementation-defined form
of the status unsuccessful termination is returned. Otherwise the status returned is
implementation-defined.

What happens if called program returns the exit code same as timeout command?

If you do i.e. a

timeout --preserve-status 1 sleep 4

you would get a status of 143, which means that the command has been terminated by SIGTERM (128+15==143). Since a command is not supposed to return values larger than 127 to communicate a status condition, this could be used in your case to disambiguate between timeout and "real" exit status.

This assumes that you are using that timeout command which comes with the GNU coreutils.



Related Topics



Leave a reply



Submit