ruby system command check exit code

后端 未结 5 562
太阳男子
太阳男子 2020-12-02 12:39

I have a bunch of system calls in ruby such as the following and I want to check their exit codes simultaneously so that my script exits out if that command fails.



        
5条回答
  •  悲哀的现实
    2020-12-02 13:13

    From the documentation:

    system returns true if the command gives zero exit status, false for non zero exit status. Returns nil if command execution fails.

    system("unknown command")     #=> nil
    system("echo foo")            #=> true
    system("echo foo | grep bar") #=> false
    

    Furthermore

    An error status is available in $?.

    system("VBoxManage createvm --invalid-option")
    
    $?             #=> #
    $?.exitstatus  #=> 2
    

提交回复
热议问题