Problem of shell script on Linux
I would like to write a small program that enables me to read a value that can be between 0 and 1FFF then use it in a shell-script.
Unfortunately, I just discovered that the return code of an executable Linux can not exceed 255. I am especially surprised that the return type of main is an int.
Code:
int main() {
return 257;
}
gives me:
Why this limitation? And-a-there a way to override?
Re: Problem of shell script on Linux
It depends on the system and shell. Usually, you recover 8 bits signed or not, depending. Do not rely on it for too large values.
Re: Problem of shell script on Linux
To retrieve more information, one solution is to write to a file or to stdout, which is quite handy if you use a pipe then.
Code:
# include <stdlib.h>
# include <stdio.h>
int main (void)
{
/* Write to standard output
(not bothered to open or close "stdout") */
fprintf (stdout, "12345");
return (0);
}
Re: Problem of shell script on Linux
the main page for POSIX exit () contains
Quote:
The value of status may be 0, EXIT_SUCCESS, EXIT_FAILURE, or any other value, though only the least significant 8 bits (that is, status & 0377) shall be available to a waiting parent process.
Re: Problem of shell script on Linux
256 values for a shell that is more than enough to take decisions (in practice, it is 0 (OK) or 1 (ERROR)). If you need to return a specific value (text), uses a text file. It can then be read by the application who needs it.
You can also get the value for stdout and stdin recover directly from another application using the 'pipe' (usually '|' in the current system ...)
Use classic MS-DOS:
Quote:
type file.txt | more