What does the || : in this line of bash script fro

2020-04-08 11:10发布

ln -s /var/log/$SERVICE_NAME $RPM_INSTALL_PREFIX/logs || :

In the rpm spec file every line ends with || :

What is the significance of the || : and why is it there?

标签: bash rpm-spec
5条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-04-08 11:52

It is simply means OR. You can try small testing like this

ls nofile-here-like || echo 'Not here'

If file not there echo will printed. Try with existing file, it will not

查看更多
Animai°情兽
3楼-- · 2020-04-08 11:53
`||` is OR operator. `:` means "do nothing". 

Your statement says, "do the soft linking or do nothing"

查看更多
做自己的国王
4楼-- · 2020-04-08 12:08

It swallows the exit code.

|| does the thing after it if the thing before it fails (i.e., has a non-zero exit code). : is the “do nothing” command. Put them together…

查看更多
成全新的幸福
5楼-- · 2020-04-08 12:09

I know others have answered, but I prefer to put:

command || /bin/true

IMHO that makes it a lot more obvious to the next person who is reading the bash script/spec file.

查看更多
等我变得足够好
6楼-- · 2020-04-08 12:10

It causes any error to be ignored so that the rpm operation isn't canceled.

|| causes the next command to run if the previous command failed, and : always succeeds.

查看更多
登录 后发表回答