Berkeley CSUA MOTD:Entry 27777
Berkeley CSUA MOTD
 
WIKI | FAQ | Tech FAQ
http://csua.com/feed/
2024/11/26 [General] UID:1000 Activity:popular
11/26   

2003/3/21 [Computer/SW/Languages/Perl] UID:27777 Activity:moderate
3/20    How do you get the error code of a system program in bash (or perl)?
        If I have in my_error.c int main (...) { exit (234); }
        how do I get that 234 in a variable?  In bash,
        ec=my_error  // sets $ec to the string "my_error"
        ec=`my_error` // sets $ec to the stdoutput of my_error
        and I get similar results in perl.  What am I doing wrong?
        \_  $  ./my_error
            $  echo $?
        \_ In perl, assuming the "hello_world" program merely prints
           the text "hello, world!":

            $foo = `./hello_world`; ## $foo now has "hello, world!"
            $exit_val = ($? >> 8);  ## $? contains the exit status as well
                                    ## as exciting flags in the low
                                    ## bits; perldoc perlvar for more.
           -geordan