本文介绍shell脚本常用命令连接符:管道符( | )、重定向( < 、>、>>、2> 、&> )、命令列表( & 、&& 、|| 、 ;、 新行 )
本文内容同微信公众号【凡登】,关注不迷路,学习上高速,欢迎关注共同学习。
进程的通信方式之一,连接多个命令,将一个命令的输出作为下一个命令的输入连接起来,实现命令之间的交互。
语法如下: 命令1 | 命令2
"|" 称为管道符 是Shell经常用到的通信工具, 将前一个应用程序的输出作为第二个应用程序的输入。
即对于Shell来说
- [@root ~]# cat | ps -f
- UID PID PPID C STIME TTY TIME CMD
- root 7898 10913 0 20:28 pts/0 00:00:00 cat # 子进程 cat
- root 7899 10913 0 20:28 pts/0 00:00:00 ps -f # 子进程 ps
- root 10913 10911 0 17:46 pts/0 00:00:00 -bash # 父进程 bash 进程
注:新开一个会话窗口, 查看上述cat子进程7898
- [@root ~]# ll /proc/7898/fd
- 总用量 0
- lrwx------ 1 root root 64 10月 12 20:45 0 -> /dev/pts/0
- l-wx------ 1 root root 64 10月 12 20:45 1 -> pipe:[2711081098]
- lrwx------ 1 root root 64 10月 12 20:45 2 -> /dev/pts/0
将输入和输出和文件建立连接; 一个进程默认会打开标准输入、标准输出、错误输出三个文件描述符
< :此处重定向输入主要是针对文件
- # 如: hello.txt文件中有 hello world
- [root@VM-0-9-centos ~]# cat < hello.txt
- hello world
-
- # 从hello.txt文件中读取数据传递给变量s
- [root@VM-0-9-centos ~]# read s < hello.txt
- [root@VM-0-9-centos ~]# echo $s
- hello world
默认是输出到终端
2.2.2、>>
将信息输出到指定文件末尾,即追加文件。
2.2.3、2>
当命令执行异常时,会把异常输出到指定文件,即异常输出。
2.2.4、&>
无论命令执行失败与否,会把命令执行的结果信息输出到指定文件,即全输出。
注:尽量避免输出到系统配置文件
- # > 输出
- [root@VM-0-9-centos ~]# echo "123" > 123.txt
- [root@VM-0-9-centos ~]# echo "456" > 123.txt
- # >> 输出
- [root@VM-0-9-centos ~]# echo "123" >> 456.txt
- [root@VM-0-9-centos ~]# echo "456" >> 456.txt
- # 查看文件
- [root@VM-0-9-centos ~]# cat 123.txt ; echo "=========" ; cat 456.txt
- 456
- =========
- 123
- 456
总结:从输出内容可以看出 > 仅保留最后一次的输出内容,即覆盖操作, >> 输出两次的输出内容,追加操作
- # 2> 演示错输出信息
- [root@VM-0-9-centos ~]# echooo "hello world output1" 2> output1.txt
- # &> 演示错误输出信息
- [root@VM-0-9-centos ~]# echooo "hello world output2" &> output2.txt
- # 打印
- [root@VM-0-9-centos ~]# cat output1.txt ; echo "======="; cat output2.txt
- bash: echooo: command not found
- =======
- bash: echooo: command not found
-
-
- # 2> 演示正确输出信息
- [root@VM-0-9-centos ~]# echo "hello world output3" 2> output3.txt
- hello world output3
- # &> 演示正确输出信息
- [root@VM-0-9-centos ~]# echo "hello world output4" &> output4.txt
- # 查看结果
- [root@VM-0-9-centos ~]# cat output3.txt ; echo "======="; cat output4.txt
- =======
- hello world output4
总结:从输出结果看,错误信息的时候都重定向输出到文件,而当命令执行正确,没异常信息时,只有&> 输出到文件
可以看到 2> 输出接受错误的信息, &> 接受全部输出信息
扩展重定向是一种将命令的输出插入到脚本中的一种方法。它使用重定向符号 > 或 >>,将命令的输出写入到文件中,从而将文件的内容插入到脚本中
- #!/bin/bash
-
- # 执行命令并将输出写入文件
- echo "echo \"Hello, World!\"" > hello.sh
-
- # 将文件内容插入到脚本中
- [root@VM-0-9-centos ~]# cat hello.sh
- echo "Hello, World!"
-
- # 执行新的脚本
- [root@VM-0-9-centos ~]# bash hello.sh
- Hello, World!
用于shell脚本生成一个指定文件
- # 编写op.sh脚本
- #!/bin/bash
- cat > /root/hello.sh <<EOF
- echo "hello world"
- EOF
-
- [root@VM-0-9-centos ~]# bash op.sh
- [root@VM-0-9-centos ~]# ll
- total 56
- -rw-r--r-- 1 root root 19 Oct 13 15:11 hello.sh
-
- # 查看生成文件
- [root@VM-0-9-centos ~]# cat hello.sh
- echo "hello world"
-
- # 执行文件
- [root@VM-0-9-centos ~]# bash hello.sh
- hello world
命令列表由 && 、|| 、 ; 、 & 符号链接起来的命令,以分号 ; 或 & 或新行结尾,
其中 && 和 || 具有相同的优先级,其次是 ; 和 & 它们也具有相同的优先级
隔开命令,第一执行完,再执行第二条,两条命令没有关系,可以使命令按照顺序执行,因此可以将多个命令组合在一行进行执行。
- if [ 2 -lt 3 ]
- then echo " 2 < 3"
- fi
- # 通过分隔符 ; 改为 一行
- if [ 2 -lt 3 ] ; then echo " 2 < 3" ; fi
-
- # 连接多个命令按顺序执行,进入root目录,将hello world内容覆盖hw.txt,查看当前文件
- [root@VM-0-9-centos ~]# cd /root ; echo "hello world" > hw.txt ; ll ; cat hw.txt
- total 60
- -rw-r--r-- 1 root root 12 Oct 13 15:35 hw.txt
- hello world
逻辑与操作符, 非短路符:& 短路符:&&
区别1:在条件语句中 & 操作符会执行链接的所有命令, && 操作符按照从左到右顺序,且在左边命令正常退出后,再依次执行后面命令
格式:command1 & command2
格式:command1 && command2
- [@root ~ ]% aaa 123 & echo "world"
- [1] 77831
- world
- [@root ~ ]% bash: command not found: aaa
- [1] + exit 127 aaa 123
- # 使用& 输出 world
-
- [@root ~ ]% aaa 123 && echo "world"
- bash: command not found: aaa
- # 使用&& 没有输出 world
区别2: & 操作置于命令后,则表示该命令在子进程中以异步方式执行,即后台方式执行,
格式:command &
- bs.sh脚本内容如下:
- #!/bin/bash
- echo "hello"
- sleep 10
- echo "world"
-
- #后台执行脚本
- [@root /tmp % bash bg.sh & echo "666"
- [1] 51610
- 666
- [@root /tmp % hello
- world
-
- [1] + done bash bg.sh
- # 从结果上看,先输出666, 输出hello , 停顿片刻,再输出world
- # echo "666" 后执行,但是先输出,
-
- [@root /tmp % bash ht.sh && echo "666"
- hello
- world
- 666
- # 从结果上看,按照顺序依次输出, 先输出hello,停顿片刻,输出hello ,最后输出666
逻辑或,
格式:command1 || command2, 当且仅当command1命令执行返回非0,command2才开始执行
- # command1 执行失败,命令2开始执行
- [@root ~ ]% aaa 123 || echo "world"
- bash: command not found: aaa
- world
-
- # command1 执行成功,命令2不执行
- [@root ~ ]% echo "hello" || echo "world"
- hello
命令连接符: 管道符( | ) 和 命令列表( & 、&& 、|| 、 ;、 新行 ) 根据命令相关关系,选择合适的连接符;
文件连接符:
输入 和 输出, 以及由此延伸出来的扩展输出和针对脚本场景下的输入输出组合 ;
其中 输入和输出针对标准的输入和输出,即控制台或文件,需要注意输出中分为标准输出和错误输出以及全输出;
扩展输出和输入输出组合针产生可执行脚本场景;
再次感谢您的阅读,欢迎关注微信公众号【凡登】共同学习。