Catching command-line errors using %x

后端 未结 4 1159
走了就别回头了
走了就别回头了 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:46

    So this doesn't directly answer your question (won't capture the command's output). But instead of trying begin/rescue, you can just check the exit code ($?) of the command:

    %x(command to run)
    unless $? == 0
       "ack! error occurred"
    end
    

    Edit: Just remembered this new project. I think it does exactly what you want:

    https://github.com/envato/safe_shell

提交回复
热议问题