Re: Most dependable way to run system commands



Steve Bertrand wrote:

> Tom Phoenix wrote:
>>
[snip]
>>
I think you're looking for the program's exit status. Traditionally on
Unix and many similar systems, the exit status is an integer, with 0
meaning "normal exit" and anything else meaning that something went
wrong. That's the value that's used by programs that run other
programs (such as the 'make' system utility) and need to know when a
step has failed. In Perl, you can access the exit status with the $?
variable after running a command via system or backticks. Beware: If
your command is run by the shell, you'll get the shell's exit status,
which may not be the exit status you were looking for.

Wow, thank you for such a thoughtful and detailed explanation! I don't
think I've ever run across the $? before (forgive me if it was noted in
the ref, obj, mod book).

Personally, I cringe whenever I see `$command` or system "$command"
inside of anything I have to work on (albeit I have several scripts I've
done this in myself, and still use). Everyone has their own way of
coding, and someone's code always looks worse to someone else. After
all, we are all only learning, no matter what we know...right?

You can also consider capturing the STDERR output of the external
program. Since the exit status doesn't say much, most programs cough
out some last words when something goes wrong. In fact, if the program
outputs anything to STDERR, even if it exited with "success", the
error text may be important news you'd want to treat as a failure. One
common way to capture STDERR when running an external program is with
IPC::Open3.

You are speaking of Unix (under shell) STDERR here, correct?

[snip]

Tom was probably thinking of Unix, but same principles apply to other
platforms.

It's also worth noting that whatever application you're using to update
the database may well output something useful to STDOUT and it may be a
simple strategy to examine this output to determine the status of an
operation. If you assign the result of a backtick string to a scalar
variable

my $status = `command`;

then the entire output of the command will be stored as a single string.
If you assign it to an array

my @status = `command`;

then the output will be stored as one line per array element.

HTH,

Rob
.



Relevant Pages

  • Re: unix script exit status while calling informix program
    ... >> Can anyone tell me why I get the different exit status for the following ... the unix are all written in KORN shell. ... >> the unix script in this case? ... INFORMIXQATERM (strings -a prog.exe | grep INFORMIXTERM) and set it to ...
    (comp.databases.informix)
  • Re: How to: End an errant program?
    ... but there exists cases that only the Terminal will let you exit. ... That are the cases when you will have to wait years until Activity Monitor ... you shouldn't infer that someone new to Mac OSX has no prior ... experience with Unix. ...
    (comp.sys.mac.misc)
  • Re: exit() values and the C standards?
    ... I've come across 2 differing definitions: ...  Under UNIX there are some file comparison commands ... In Unix general wisdom is 0 means success, 1 failure, and if you ... use some other smallish values as exit codes. ...
    (comp.lang.c)
  • should these be fixed for python 2.4?
    ... pydoc's handling of overriden methods without docstrings: ... platform-independent way to run a process that possibly ... stdout and stderr as strings and the exit status. ... reliably and is unix only. ...
    (comp.lang.python)
  • Re: User-defined exit values
    ... As I know that there are some exit values are reserved by the Unix, ... With ksh88 you have the ranges that you mentioned above, and the exit codes ... have also predefined meanings (command not found, ...
    (comp.unix.shell)