简体   繁体   中英

How to negate a program's error code for an if statement in fish shell?

I wanted to do something like either of these statements, that is probably like bash, but received errors:

if ! prog ...
  ....

Error: fish: Unknown command '! prog ...' fish: Unknown command '! prog ...'

if test ! prog ...
  ....

Error: test: Expected a combining operator like '-a' at index 2

You're looking for the not keyword:

if not false
   echo Foo
end

Ended up just using $status .

if test "$status" != "0"
  ...

I don't like to separate the statement from where I check its error code in case of future accidental code maintenance inserts other statements in between, even as nearly harmless as echo , but I could not find another way to use a program call on the test line to get the negated condition I wanted.

I guess I could wrap some of that 'status' and negating logic into a function, but that just seems like more work, and I'm hoping I'm missing a more succinct syntax as a conditional statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM