您当前的位置:首页 > 计算机 > 系统应用 > Linux

CentOS 7 系统下连续执行多条命令方法

时间:09-19来源:作者:点击数:

CentOS7下多条命令可以放在一行上面执行,其执行情况得依赖于在命令之间的分隔符。

如果每条命令被一个分号 (;) 分隔,无论中间是否有错误的命令,命令会连续的执行下去,直到执行完毕,如:

[root@node1 ~]# printf "%s\n" "First command" ; date ; printf "%s\n" "Third command"

First command

Mon Oct 19 10:28:31 CST 2020

Third command

[root@node1 ~]# printf "%s\n" "First command" ; data ; printf "%s\n" "Third command"

First command

-bash: data: command not found

Third command

如果每条命令被 两个与号(&&)分隔,如果中间有错误的命令,则不再执行后面的命令,没错则执行完毕为止,如:

[root@node1 ~]# printf "%s\n" "First command" && date && printf "%s\n" "Third command"

First command

Mon Oct 19 10:27:56 CST 2020

Third command

[root@node1 ~]# printf "%s\n" "First command" && data && printf "%s\n" "Third command"

First command

-bash: data: command not found

如果每条命令双竖线(||)分隔,如果命令遇到可以成功执行的命令,那么命令停止执行,即使后面还有正确的命令而后面的所有命令都不到执行;假如命令一开始就执行失败,那么就会执行 || 后的下一条命令,直到遇到有可以成功执行的命令为止;假如所有命令的都失败,则所有这些失败的命令都会被尝试执行一次,如:

[root@node1 ~]# printf "%s\n" "First command" || date || printf "%s\n" "Third command"

First command

[root@node1 ~]# data || printf "%s\n" "Second command" || printf "%s\n" "Third command"

-bash: data: command not found

Second command

[root@node1 ~]# data || lll || lls

-bash: data: command not found

-bash: lll: command not found

-bash: lls: command not found

[root@node1 ~]# data || lll || lls || printf "%s\n" "Fourth command"

-bash: data: command not found

-bash: lll: command not found

-bash: lls: command not found

Fourth command

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐