From: mike@... (Mike Stok) Date: 2002-03-06T23:52:32+09:00 Subject: Re: Improvement to system() In article , 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 > > Quickly, why did the first one returns false, and why did the second one > returns false? > > Did you instantly catch the typo? Sometimes the command to be executed > is determined at some other place. Thus, if system(*cmd) returns > false, there's no easy way to determine why it fails. > > A solution is to use File.stat.executable? But even directories are > executable. > > Any better solution? You can look at $? e.g. irb(main):001:0> $? nil irb(main):002:0> system "/bin/false" false irb(main):003:0> $? 256 irb(main):004:0> system "/bin/true" true irb(main):005:0> $? 0 irb(main):006:0> system "/bin/banana" false irb(main):007:0> $? 32512 The contents of $? can be viewed as 2 8 bit values, exit_status = ($? & 0xff00) >> 8 exit_flags = $? & 255 (gory details can be found in various header files like /usr/include/bits/waitstatus.h on linux) If the program you were calling really did an "exit(127)" then you might have to dig deeper, but $? contains some extra information, Hope this helps. Mike -- mike@stok.co.uk | The "`Stok' disclaimers" apply. http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA mike@starnix.com | Fingerprint 0570 71CD 6790 7C28 3D60 http://www.starnix.com/ | 75D2 9EC4 C1C0 0599 13DA