问题
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