管道和重定向分号-连接符
本文介绍shell脚本常用命令连接符:管道符( | )、重定向( < 、>、>>、2> 、&> )、分号( ; )
本文内容同微信公众号【凡登】,关注不迷路,学习上高速,欢迎关注共同学习。
1、管道
进程的通信方式之一,连接多个命令,将一个命令的输出作为下一个命令的输入连接起来,实现命令之间的交互。
语法如下: 命令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
2、重定向
将输入和输出和文件建立连接; 一个进程默认会打开标准输入、标准输出、错误输出三个文件描述符
2.1、输入
< :此处重定向输入主要是针对文件
# 如: 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> : 当命令执行异常时,会把异常输出到指定文件,即异常输出。
- &> : 无论命令执行失败与否,会把命令执行的结果信息输出到指定文件,即全输出。
注:尽量避免输出到系统配置文件
演示 > 和 >> 输出重定向
# > 输出
[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> 输出接受错误的信息, &> 接受全部输出信息
2.3、扩展输出
扩展重定向是一种将命令的输出插入到脚本中的一种方法。它使用重定向符号 > 或 >>,将命令的输出写入到文件中,从而将文件的内容插入到脚本中
#!/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!
2.4、输入和输出的组合
用于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
3、分号( ; )
隔开命令,第一执行完,再执行第二条,两条命令没有关系,可以使命令按照顺序执行,因此可以将多个命令组合在一行进行执行。
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