开发者

Create a file when the command succeed. Create another when it fails. One line?

开发者 https://www.devze.com 2023-04-10 03:20 出处:网络
In Bash, I want to create a file when the command succeed, otherwise create another file. Something along the 开发者_运维知识库line of

In Bash, I want to create a file when the command succeed, otherwise create another file. Something along the 开发者_运维知识库line of

if `my command`; then
  touch command.complete
else
  touch command.fail
fi

Is it possible to write this in one line?


In most cases this will do:

my.command && touch command.complete || touch command.failed

The corner case is, thanks to @uzsolt, when touch complete itself can fail. Then touch failed is executed. In case the difference matters, you can use this one:

my.command && (touch command.complete || true) || touch command.failed
0

精彩评论

暂无评论...
验证码 换一张
取 消