A
Ara.T.Howard
this seems broken:
~/tmp > cat a.rb
STDERR.reopen STDOUT
STDOUT.sync
DIV = '=' * 79
commands = [
'no-such-command',
'ls --no-such-option',
'ls .',
]
commands.each do |command|
[false, true].each do |redirect|
fork do
puts DIV
command << " >/dev/null 2>&1" if redirect
puts "command <#{ command }>"
system command
puts "exit_status <#{ $?.exitstatus }>"
end
Process.wait
end
end
~/tmp > ruby a.rb
===============================================================================
command <no-such-command>
a.rb:18:in `system': No such file or directory - no-such-command (Errno::ENOENT)
from a.rb:18
from a.rb:13:in `fork'
from a.rb:13
from a.rb:13:in `each'
from a.rb:12
from a.rb:12:in `each'
from a.rb:11
from a.rb:11
===============================================================================
command <no-such-command >/dev/null 2>&1>
exit_status <127>
===============================================================================
command <ls --no-such-option>
ls: unrecognized option `--no-such-option'
Try `ls --help' for more information.
exit_status <1>
===============================================================================
command <ls --no-such-option >/dev/null 2>&1>
exit_status <1>
===============================================================================
command <ls .>
a.rb
exit_status <0>
===============================================================================
command <ls . >/dev/null 2>&1>
exit_status <0>
the above behaviour of system means the following rules must be followed when
making system calls:
- iff stderr IS NOT redirected you must be prepared to catch Errno::ENOENT in
the event your command is not found.
- iff stderr IS redirected you will NOT be informed of non-existent commands
via an exception and must check $?.exitstatus manually for 127 to
determine if the command was not found
- in all cases you must check $?.exitstatus for success since your command
may be found, but fail for other reasons and this will not raise any
errors.
so, in summary you must still always check $?, but sometimes errors will be
thrown depending on the contents of the string passed to system. am i the
only one who finds this confusing? it seems like any code using system will
turn into spaghetti if it follows these rules...
-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| A flower falls, even though we love it;
| and a weed grows, even though we do not love it.
| --Dogen
===============================================================================
~/tmp > cat a.rb
STDERR.reopen STDOUT
STDOUT.sync
DIV = '=' * 79
commands = [
'no-such-command',
'ls --no-such-option',
'ls .',
]
commands.each do |command|
[false, true].each do |redirect|
fork do
puts DIV
command << " >/dev/null 2>&1" if redirect
puts "command <#{ command }>"
system command
puts "exit_status <#{ $?.exitstatus }>"
end
Process.wait
end
end
~/tmp > ruby a.rb
===============================================================================
command <no-such-command>
a.rb:18:in `system': No such file or directory - no-such-command (Errno::ENOENT)
from a.rb:18
from a.rb:13:in `fork'
from a.rb:13
from a.rb:13:in `each'
from a.rb:12
from a.rb:12:in `each'
from a.rb:11
from a.rb:11
===============================================================================
command <no-such-command >/dev/null 2>&1>
exit_status <127>
===============================================================================
command <ls --no-such-option>
ls: unrecognized option `--no-such-option'
Try `ls --help' for more information.
exit_status <1>
===============================================================================
command <ls --no-such-option >/dev/null 2>&1>
exit_status <1>
===============================================================================
command <ls .>
a.rb
exit_status <0>
===============================================================================
command <ls . >/dev/null 2>&1>
exit_status <0>
the above behaviour of system means the following rules must be followed when
making system calls:
- iff stderr IS NOT redirected you must be prepared to catch Errno::ENOENT in
the event your command is not found.
- iff stderr IS redirected you will NOT be informed of non-existent commands
via an exception and must check $?.exitstatus manually for 127 to
determine if the command was not found
- in all cases you must check $?.exitstatus for success since your command
may be found, but fail for other reasons and this will not raise any
errors.
so, in summary you must still always check $?, but sometimes errors will be
thrown depending on the contents of the string passed to system. am i the
only one who finds this confusing? it seems like any code using system will
turn into spaghetti if it follows these rules...
-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| A flower falls, even though we love it;
| and a weed grows, even though we do not love it.
| --Dogen
===============================================================================