[20180930]bash shell &.txt
--//今天写bash shell脚本,本来写2个&,少写1个.开始不理解为什么能执行?
--//通过例子说明:
--//首先说明一下 && ||
--// && 表示与的意思,也就是命令1 执行成功,才执行命令2 .如果命令1执行失败,命令2不会执行.
--// || 第1个执行成功,后面不执行. 第1个执行不成功,后面执行.
Command1 && command2
The second command is only started if the first command is successful. To achieve this, the shell checks the exit
(return) status of the first command and starts the second command only if and when that exit status is found to be "0".
Command1 || command2
The second command is only started if the first command fails. The shell checks the exit status of the first command and
starts the second command only if that exit status is not equal to "0".
--//例子:
$ date1 && echo ok
sh.exe: date1: command not found
--//因为没有data1命令,报错!!第2个命令不执行.
$ date1 || echo fail
sh.exe: date1: command not found
fail
--//如果写成如下:
$ date && echo ok
Sun Sep 30 20:13:40 2018
ok
--//如果写成如下:
$ date & echo ok
[1] 4860
ok
Sun Sep 30 20:13:52 2018
[1]+ Done date
--//我发现居然也执行,一看看很容易明白. & 相当于 放入后台执行.
--//实际上 单独 1个& 表示第一个命令放在后台执行,第二个命令在前台执行.
--//这里 & 还有分隔2个命令的作用.有时候不小心理解错误.做一个记录.
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/267265/viewspace-2215376/
,如需转载,请注明出处,否则将追究法律责任。