How to get the status of previous command in fish script

天涯浪子 提交于 2019-12-12 04:00:08

问题


Say I want to do something in multi commands in a fish script:

do_some_important_things

if /* previous command succeeds*/
   echo Good
else
   echo Failed
end

I'm not sure how to write the /* previous command succeeds*/ part. I tried $status, but it give me an warning:

fish: Variables may not be used as commands. Instead, define a function like 'function status; 0 $argv; end' or use the eval builtin instead, like 'eval $status'. See the help section for the function command by typing 'help function'. /Users/freewind/Downloads/bbb/m (line 50): if $status


回答1:


Use the test command and the status variable:

if test $status -eq 0



回答2:


This is a very common use case, so there is a command to achieve this easily: and.

Assume you want to call foo and afterwards, if and only if it succeeded (exit status equals 0) you want to call bar. Then you can simply write

foo; and bar


来源:https://stackoverflow.com/questions/34721446/how-to-get-the-status-of-previous-command-in-fish-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!