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

 admin   2022-08-02 15:24   394 人阅读  0 条评论

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

文章转载于:https://blog.csdn.net/qq_23689053/article/details/109155595

http://ynu.xbcloud.top:9908
本文地址:http://zxbblog.com/?id=824
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!

 发表评论


表情

还没有留言,还不快点抢沙发?