C program RETURN code to a SHELL

0 visualitzacions
Ves al primer missatge no llegit

SebastiaNot

no llegida,
15 de nov. 2011, 10:58:4215/11/11
a unix-basics
Hi. I have a C program that ends with the lines
+ + +
printf ( "Sample code end - RC [%d].\n", iERC ) ;
return ( iERC ) ;
+ + +

integer iERC is assigned a queue depth, lets say 10.000 ;
and the C program is called from a shell like this :

+ + +
#!/bin/bash
../subdir/mycode PARAM1
myrc=$?
echo "MyRC is" $myrc
+ + +

To my surprise, my program displays a correct exit value (as 10.000)
but the value captured by the shell is not the same .... (it displays
"80")

Any clue ? Sebastian.

Cameron Simpson

no llegida,
15 de nov. 2011, 15:33:2215/11/11
a unix-...@googlegroups.com

The return from main() is handed to exit(). From "man 3 exit" (macOSX,
but other UNIX are essentially the same):

[...]
void exit(int status);
[...]
make the low-order eight bits of the status argu- ment available
to a parent process which has called a wait(2)-family function.

You don't get the unadorned "return" value. The exit status of a program
is small integer with some signal information folded in. It is essentially
meant for program control flow ("did the program succeed or fail, was it
killed by a signal").

If you want to return arbitrary information from a program, print it to
the standard output or so a file.

Cheers,
--
Cameron Simpson <c...@zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

Think of it as evolution in action. - Tony Rand, _Oath of Fealty_

SebastiaNot

no llegida,
16 de nov. 2011, 8:49:0516/11/11
a unix-basics

Hi, Cameron - thanks for your help.
Excuse my english, but ...

Do you mean only 8 bits of my return code "iERC" are passed to the
surrounding shell ?

So, if the queue depth is larger than 256 ... I will loose the higher
bits ?

Ok - I can try to verify this !!!!!

I shall run my code with 257 messages, to see if I get a "2" value
back ...

Many thanks, Cameron, again.

It is a pleasure to feel and know there are people available to help
you. Cheers. Sebastian.




On 15 nov, 21:33, Cameron Simpson <c...@zip.com.au> wrote:
> Cameron Simpson <c...@zip.com.au> DoD#743http://www.cskk.ezoshosting.com/cs/

Cameron Simpson

no llegida,
16 de nov. 2011, 17:02:5516/11/11
a unix-...@googlegroups.com
On 16Nov2011 05:49, SebastiaNot <sebast...@gmail.com> wrote:
| Do you mean only 8 bits of my return code "iERC" are passed to the
| surrounding shell ?

Yes.

| So, if the queue depth is larger than 256 ... I will loose the higher
| bits ?

Larger that 255 will do it.

The program exit status is more about success/failure logic than the
program result. The program result typically goes to the standard
output.

Typical example:

if grep foo some-file >grep.out
then
echo 'lines containing "foo" are in the file "grep.out"'
else
echo 'no lines contained "foo"'
fi

The program (grep) result went to the file "grep.out".
The program exit status is zero (success) if some lines were found
and non-zero (usually the value 1) if no lines were found.

So grep has a "return 0" if it finds lines and "return 1" if not.

The other nonzero values you can use with return (or the exit()
function) can be used if you want to distinguish different failure
modes. Personally I usually use 1 for "normal" failure and 2 for "bad
invocation" such as using invalid command line options.

Various other programs may have a small suite of non-zero values for
particular error conditions.

Cheers,
--
Cameron Simpson <c...@zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

Ode to a Squid:
Half a mile, half a mile,
Half a mile onward,
All in the Valley of Death
Rode the Six Hundred.
Copyright Tennyson/Richards(tm)
- Jan Richards <Jan_Ri...@dge.ceo.dg.com>

SebastiaNot

no llegida,
24 de nov. 2011, 7:37:4924/11/11
a unix-basics

Verified - you are right :

I put 261 messages in the queue = 256 + 5 ... and the "myrc=$?" value
is "5" ....

Cheers, man !

+++
Sample INQQD end - RC [261]. <<<<<<<<<<<<<< real queue
depth, before "return()"
MyRC is 5 <<<<<<<<<<<<<<
value captured in $?
+++

On 16 Nov, 23:02, Cameron Simpson <c...@zip.com.au> wrote:

> Cameron Simpson <c...@zip.com.au> DoD#743http://www.cskk.ezoshosting.com/cs/


>
> Ode to a Squid:
>  Half a mile, half a mile,
>    Half a mile onward,
>  All in the Valley of Death
>    Rode the Six Hundred.
> Copyright Tennyson/Richards(tm)

>         - Jan Richards <Jan_Richa...@dge.ceo.dg.com>

Respon a tots
Respon a l'autor
Reenvia
0 missatges nous