-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Sunday 26 January 2003 01:12 pm, exits funnel wrote:
> Hello,
>
> I've written the following simple program:
>
> #include <cstdlib>
>
> int main( )
> {
>   // /fff does not exist
>   int return_value = system("ls -l /fff");
>   cout << "\n Return Value: " << return_value << "\n";
>   return return_value;
> }
>
> When I compile this and run it from bash I see "Return
> Value: 256" at stdout as I'd expect.  However, I then
> immediately 'echo $?' and bash spits back '0' rather
> than '256.'  Is this correct?  Shouldn't $? hold the
> return value of the last program run from bash (ie, my
> c program?)  Thanks in advance for any replies.

I'm not a coder. That doesn't stop me from trying, though. ;)

I can't even make the above code compile here.
(Red Hat 8.0, gcc-3.2)
$ g++ -o return return.cpp
return.cpp: In function `int main()':
return.cpp:7: `cout' undeclared (first use this function)

That said, it appears bash only handles return values up to and including 
255. See below.

I did this in C as a test:

$ cat return.c
#include <stdio.h>

int main( )
{
  int return_value = system("ls -l /fff");
  printf("\n Return Value: %i\n", return_value);
  return return_value;
}

[mfratoni@paradox mfratoni]$ gcc -o return return.c
[mfratoni@paradox mfratoni]$ ./return
ls: /fff: No such file or directory

 Return Value: 256
[mfratoni@paradox mfratoni]$ echo $?
0

However, changing the return value to a smaller value, it works.
$ cat return.c
#include <stdio.h>

int main( )
{
  int return_value = system("ls -l /fff");
  printf("\n Return Value: %i\n", return_value);
  return (return_value - 1);
}

[mfratoni@paradox mfratoni]$ gcc -o return return.c
[mfratoni@paradox mfratoni]$ ./return
ls: /fff: No such file or directory

 Return Value: 256
[mfratoni@paradox mfratoni]$ echo $?
255

- -- 
- -Michael

pgp key:  http://www.tuxfan.homeip.net:8080/gpgkey.txt
Red Hat Linux 7.{2,3}|8.0 in 8M of RAM: http://www.rule-project.org/
- --
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)

iD8DBQE+NG3Bn/07WoAb/SsRAi91AKCVbgj3ewAEYZvVQrwLuDmAuio1tgCgiJyG
8kqlru15U7CZ3WRXI7G19Bk=
=L+qA
-----END PGP SIGNATURE-----



-- 
redhat-list mailing list
unsubscribe mailto:[EMAIL PROTECTED]?subject=unsubscribe
https://listman.redhat.com/mailman/listinfo/redhat-list

Reply via email to