Catching command-line errors using %x

后端 未结 4 1189
走了就别回头了
走了就别回头了 2021-01-11 13:16

Whenever you want to execute something on the command line, you can use the following syntax:

%x(command to run)

However, I want to catch a

4条回答
  •  余生分开走
    2021-01-11 13:33

    You need a mix of @Cam 's answer and @tonttu 's answer.

    decent explanation of $? and others.

    Edit: the domain http://blog.purifyapp.com is now in hands of a domain-squatter and scammer.

    result = %x(command to run 2>&1)
    
    unless $? == 0 #check if the child process exited cleanly.
        puts "got error #{result}"
    end
    

提交回复
热议问题