重定向-管道技术-xargs命令详解


重定向


什么是重定向?

将原本要输出在屏幕的内容,重新定向输出到指定的文件或设备中。

为什么要使用重定向?

1、备份的时候需要知道备份的结果。

2、屏幕上输出信息比较重要的时候需要保存下来。

3、定时任务我们需要知道结果。

4、执行命令时,明知会报错,我们会使用重定向,将结果放入 /dev/null

5、执行命令时,正确的结果和错误会同时输出,将正确的结果输入到常规日志,将错误的结果输出到错误日志。


重定向的分类


  • 标准输入
  • 标准输出
名称 文件描述符 作用
标准输入(stdin) 0 通常键盘(其它的输入设备)
标准输出(stdout) 1 默认输出到屏幕
错误输出(stderr) 2 默认输出到屏幕
文件名(filename) 3+

输出重定向


/dev/null

/dev/pts/0

/tmp/1.txt

类型 符号 用途 备注
标准覆盖输出重定向 1> 将命令执行的正确结果默认输出位置修改为指定的文件或者目录中。 通常>即可,1可以不写
标准追加输出重定向 >> 将命令的输出的正确结果,输出到指定文件的末尾,不会覆盖原文件的内容。
错误输出重定向 2> 将命令执行的错误结果输出到默认位置,修改为指定的文件或者终端(覆盖原有的内容)
错误追加输出重定向 2>> 将命令执行的错误结果输出到指定的文件末尾或者终端(不会覆盖原有的内容)
标准输入重定向 0< 将命令中接收输入的内容默认由键盘,改为命令或者文件。 通常0可以省略不写,默认0
标准追加输入重定向 0<< 将命令中接收输入内容默认由键盘,改为命令或者文件。
# 标准覆盖输出重定向
[root@oldboy /data]# echo 123 > a.txt	# 输出到文件
echo '123' >/dev/pts/0					# 输出到终端

# 标准输出追加重定向
[root@oldboy /data]# echo 123 >> a.txt
[root@oldboy /data]# echo gongxiaoliao >> /var/log/messages

# 错误输出重定向
ll /sfdsfa 2> a.txt

# 正确的和错误的都输出到同一个文件当中
- 2>&1
- &>		效率更高
[root@oldboy /data]# ll /root /sfdsfa > a.txt 2>&1
[root@oldboy /data]# ll /root /sfdsfa &> a.txt

# 错误输出到黑洞
ls /root /asdfds 2> /dev/null

# 脚本中使用重定向
#!/bin/bash

. /etc/init.d/functions
read -p "请输入要检测的IP:" IP
ping -c1 -W1 $IP &>/dev/null
if [ $? -eq 0 ];then
        action "$IP" /bin/true >> /tmp/IP_OK.txt
else
        action "$IP" /bin/false >> /tmp/IP_FAILD.txt
fi
注:echo 123>1.txt 123是文件描述符;echo aa1>2.txt 输出的是aa1

管道符:将管道符左边命令的标准输出交给管道符右边的命令处理。


输入重定向


# 比如tr命令
root@oldboy ~]# tr 'a' 'A' </etc/passwd
mail -s 'biaoti' 邮件地址 < /etc/passwd
echo 123asdf | mail -s 'biaoti' 邮件地址

管道技术


管道操作的符号: | 管道符

将管道符左边命令的标准输出,交给管道符右边命令的标准输入处理。管道符只接收正确的输出。

用法: cmd1|cmd2|cmd3

案例一

[root@oldboy ~]# sort -t: -k3 -nr /etc/passwd|head -5 # 对数据的排序处理

案例二

# 统计出passwd文件中,所有用户的shell种类
[root@gong ~]# awk -F: '{print $7}' /etc/passwd|sort |uniq|wc -l
5
[root@gong ~]# cut -d':' -f7 /etc/passwd|sort|uniq -c|sort -nr

案例三

10种方法,取IP
[root@gong ~]# hostname -I
10.0.0.89
[root@gong ~]# ip a s eth0|head -3|tail -1|cut -d'/' -f1|cut -d' ' -f6
10.0.0.89
[root@gong ~]# ip a s eth0|grep -w inet|cut -d'/' -f1|cut -d' ' -f6
10.0.0.89
[root@gong ~]# ip a s eth0|sed -n 3p |cut -d'/' -f1|cut -d' ' -f6
10.0.0.89
[root@gong ~]# ip a s eth0|sed -n /inet/p |cut -d'/' -f1|cut -d' ' -f6|head -1
10.0.0.89
[root@gong ~]# ip a s eth0|sed '1,2d' |sed '2,$d'|cut -d' ' -f6|cut -d'/' -f1
10.0.0.89
[root@gong ~]# ip a s eth0|sed -nr 's#.*inet (.*)/24.*#1#g'p
10.0.0.89
[root@gong ~]# ip a s eth0|awk 'NR==3{print $0}'|cut -d' ' -f6|cut -d'/' -f1
10.0.0.89
[root@gong ~]# ip a s eth0|awk 'NR>2&&NR<4{print $0}'|cut -d' ' -f6|cut -d'/' -f1
10.0.0.89
[root@gong ~]# ip a s eth0 |awk -F'[ /]+' 'NR==3{print $3}'
10.0.0.89
[root@gong ~]# ip a s eth0|sed -n 3p|awk -F'[ /]+' '{print $3}'
10.0.0.89



案例四

# 取出磁盘所剩百分比
[root@gong ~]# df -h|awk 'NR>1{print $5}'
6%
0%
0%
2%
0%
25%
0%

管道技术-tee

在重定向到文件或者设备的时候还可以向终端输出。

# tee 和 重定向的区别
[root@zls ~]# date > date.txt 
[root@zls ~]# date |tee date.txt

xargs


[root@localhost opt]# find /etc/ -name '*.conf'|xargs cp -t /tmp/

## 2.xargs和管道符的不同

[root@gong /tmp]# touch file1.txt
	[root@gong /tmp]# touch file2.txt
	[root@gong /tmp]# touch file3.txt
	[root@gong /tmp]# echo abc >file1.txt 
	[root@gong /tmp]# echo abb >file2.txt 
	[root@gong /tmp]# echo abd >file3.txt 
	[root@gong /tmp]# ll |sed 's#file#abc#g'		# 在不使用xargs的时候,sed更改的只是输出的结果
	total 12
	-rw-r--r-- 1 root root 4 Apr  8 16:40 abc1.txt
	-rw-r--r-- 1 root root 4 Apr  8 16:40 abc2.txt
	-rw-r--r-- 1 root root 4 Apr  8 16:40 abc3.txt
	[root@gong /tmp]# ll
	total 12
	-rw-r--r-- 1 root root 4 Apr  8 16:40 file1.txt
	-rw-r--r-- 1 root root 4 Apr  8 16:40 file2.txt
	-rw-r--r-- 1 root root 4 Apr  8 16:40 file3.txt
	[root@gong /tmp]# 
	[root@gong /tmp]# cat file*
	abc
	abc
	abd
	[root@gong /tmp]# ls |xargs sed -i 's#ab#AB#g'	# 在使用xargs的时候会把前面的内容当作单个的文件再被sed处理,实际上文件内容已经被更改。
	[root@gong /tmp]# cat file*
	ABc
	ABc
	ABd

原文地址:https://www.cnblogs.com/gshelldon/p/13276138.html