ruby system command check exit code

后端 未结 5 559
太阳男子
太阳男子 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:34

    You're not capturing the result of your system call, which is where the result code is returned:

    exit_code = system("ruby test.rb")
    

    Remember each system call or equivalent, which includes the backtick-method, spawns a new shell, so it's not possible to capture the result of a previous shell's environment. In this case exit_code is true if everything worked out, nil otherwise.

    The popen3 command provides more low-level detail.

提交回复
热议问题