Piping and redirection
只有在开水里,茶叶才能展开生命浓郁的香气.
One, redirect
标准输入、标准正确输出、标准错误输出
进程在运行的过程中根据需要会打开多个文件,每打开一个文件会有一个数字标识。这个标识叫文件描述符。
进程使用文件描述符来管理打开的文件(FD----file descriptors).
文件描述符:每打开一个程序都会有文件描述
0, 1, and 2, known as standard input, standard output, and standard error
0,标准输入(键盘)
1,标准输出
2,标准错误,
3+,进程在执行过程中打开的其他文件。
&:表示正确错误混合输出
2. Output redirection (overwrite, append)
> ----覆盖
>> ----追加
正确输出: 1> 1>> 等价于 > >>
错误输出: 2> 2>>
2.1. Case 1: Output redirection (override)
[root@linux-server ~]# date 1> date.txt #正确输出--覆盖
# 注意:如果 > 前面什么都不加默认为1,标准正确输出。
2.2. Case 2: Output redirection (append)
[root@linux-server ~]# date >> date.txt #正确输出--追加
2.3. Case 3: Error output redirection
[root@linux-server ~]# ls /home/ /aaaaaaaaa >list.txt
ls: cannot access /aaaaaaaaa: No such file or directory
[root@linux-server ~]# ls /home/ /aaaaaaaaa >list.txt 2>error.txt #重定向到不同的位置
[root@linux-server ~]# cat error.txt
ls: cannot access /aaaaaaaaa: No such file or directory
2.4. Both right and wrong are entered in the same place
[root@linux-server ~]# ls /home/ /aaaaaaaaa &>list.txt #混合输出到相同文件
2.5. Redirect to empty device /dev/null
[root@linux-server ~]# ls /home/ /aaaaaaaaa >list.txt 2>/dev/null #空设备,将错误的输出丢掉
[root@linux-server ~]# ls /home/ /aaaaaaaaa &>/dev/null #空设备,将正确与错误的输出丢掉
2.6. Use redirection in scripts
Actual combat one (without using redirection)
[root@linux-server ~]# vim ping1.sh
#!/bin/bash
ping -c1 10.18.40.100
if [ $? -eq 0 ];then
echo "10.18.40.100 is up."
else
echo "10.18.40.100 is down!"
fi
[root@linux-server ~]# chmod +x ping1.sh
[root@linux-server ~]# ./ping1.sh #执行文件(执行脚本)
PING 10.18.40.100 (10.18.40.100) 56(84) bytes of data.
--- 10.18.40.100 ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms
10.18.40.100 is down!
Actual combat two (using redirection)
[root@linux-server ~]# vim ping1.sh
#!/bin/bash
ping -c1 10.18.40.100 &>/dev/null
if [ $? -eq 0 ];then
echo "10.18.40.100 is up." >>up.txt
else
echo "10.18.40.100 is down!" >>down.txt
fi
[root@linux-server ~]# ./ping1.sh
Three, input redirection<
标准输入: < 等价 0<
语法:cat >> file5 <<EOF #可以写到脚本或者文件里面
EOF:开始和结束的标记。
成对使用
结尾的另一个必须定格写。
echo会将输入的内容送往标准输出(打印)
echo 内容 >> 文件名或脚本里面
Actual case 1:
[root@linux-server ~]# cat >file4 <<EOF
> 111
> 222
> 333
> 444
> EOF
[root@linux-server ~]# cat file4
111
222
333
444
Actual case two
Use redirection to create a multi-line file script to create a multi-line file
[root@linux-server ~]# vim create_file.sh
#!/bin/bash
cat >file200.txt <<EOF
111
222
333
yyy
ccc
EOF
[root@linux-server ~]# chmod +x create_file.sh
[root@linux-server ~]# ./create_file.sh
[root@linux-server ~]# cat file200.txt
111
222
333
yyy
ccc
Fourth, the pipeline |
用法:command1 | command2 |command3 |...
Actual case one
[root@linux-server ~]# rpm -qa |grep 'httpd' #查询所有安装的软件包,过滤包含httpd的包
httpd-tools-2.4.6-90.el7.centos.x86_64
httpd-2.4.6-90.el7.centos.x86_64
Actual case two
Sort users in /etc/passwd by UID size
[root@linux-server ~]# sort -t":" -k3 -n /etc/passwd #以: 分隔,将第三列按字数升序
[root@linux-server ~]# sort -t":" -k3 -n /etc/passwd -r #以: 分隔,将第三列按字数降序
[root@linux-server ~]# sort -t":" -k3 -n /etc/passwd |head #以: 分隔,将第三列按字数升序看前十行
[root@linux-server ~]# sort -t":" -k3 -n /etc/passwd |tail #以: 分隔,将第三列按字数升序看后十行
参数详解:
sort 排序,默认升序
-t 指定分隔符
-k 指定列
-n 按数值
-r 降序
head 默认输出前十行
head -15 默认输出前十五行
tail 默认输出后十行
tail -20 默认输出后十五行
Five, parameter transfer: xargs
对:ls cp rm 管道不能执行。所以通过xargs。
cat a.txt | xargs -i cp {} /目录
{}:前面传过来的内容
-i :为了让大括号生效
目录时 -r
解释:前面传过来的东西交给大括号
cat file.txt |xargs ls -l
前面是目录或者目录的路径。 ls - l 后面可以不加大括号,直接执行。
Actual case one
[root@linux-server ~]# touch /home/file{1..5}
[root@linux-server ~]# vim files.txt
/home/file1
/home/file2
/home/file3
/home/file4
/home/file5
[root@linux-server ~]# cat files.txt |ls -l #不加xargs传参,看输出结果
[root@linux-server ~]# cat files.txt |rm -rvf #不加xargs传参,看输出结果
[root@linux-server ~]# cat files.txt |xargs ls -l
-rw-r--rwx. 1 root root 12 Nov 7 21:57 /home/file1
-rw-r--r--. 1 root root 0 Nov 7 21:57 /home/file2
-rw-r--r--. 1 root root 0 Nov 7 21:57 /home/file3
-rw-r--r--. 1 root root 0 Nov 7 21:57 /home/file4
-rw-r--r--. 1 root root 0 Nov 7 21:57 /home/file5
[root@linux-server ~]# cat files.txt |xargs rm -rvf
removed ‘/home/file1’
removed ‘/home/file2’
removed ‘/home/file3’
removed ‘/home/file4’
removed ‘/home/file5’
Actual case two
[root@linux-server ~]# touch /home/file{1..5}
[root@linux-server ~]# # cat files.txt | xargs -i cp -rvf {} /tmp/
‘/home/file1’ -> ‘/tmp/file1’
‘/home/file2’ -> ‘/tmp/file2’
‘/home/file3’ -> ‘/tmp/file3’
‘/home/file4’ -> ‘/tmp/file4’
‘/home/file5’ -> ‘/tmp/file5’