On Wed, Mar 06, 2002 at 11:07:49PM +0900, Yohanes Santoso wrote:
> Hi,
> 
> I just got bitten by return value of system() because it does not
> distinguish invalid command and error code resulted from executing
> command.
>
> irb(main):014:0> system "/bin/falsed" 
> false
> irb(main):015:0> system "/bin/false" 
> false

IMO, It's a problem with system() itself and not with the
implementation, since sometimes it calls /bin/sh to execute the program.
In that case,

  system("/bin/falsed '' 2>/dev/null")
  system("/bin/false '' 2>/dev/null")

will both return false, even if you check for an invalid executable,
since on *nix, both of these commands require using /bin/sh to execute
the command (due to the tick marks).

I'm not sure how portable/reliable it is, but $? does contain the return
value from executing the command.  On my system, I get the following:

  irb(main):018:0> system("/bin/false"); $?
  256
  irb(main):019:0> system("/bin/falsed"); $?
  32512
  irb(main):020:0> system("/bin/false '' 2>/dev/null"); $?
  256
  irb(main):021:0> system("/bin/falsed '' 2>/dev/null"); $?
  32512

IMO, it would be nice if system() were consistent about calling a
subshell; upon a quick inspection of process.c, it looks like on some
systems it always calls a shell, and on other systems it only calls a
shell when there are shell characters in the string.  Inconsistency is
often a bad thing.

Hope this helps,

Paul