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

作者:admin 发表于:2022-08-02

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

版权声明

本站部份资源来自本人原创、以及网络收集,经过测试整理,版权争议与本站无关。
所有资源仅限用于学习和测试研究,不得用于商业或者非法用途,否则、一切后果请用户自负。
网络技术更新极快,我们不保证资源的长久可用性,下载前请仔细查看发布日期。

分享:

扫一扫在手机阅读、分享本文