第401集系统管理-Linux重定向与管道 | 字数总计: 4.4k | 阅读时长: 19分钟 | 阅读量:
系统管理 - Linux重定向与管道 1. 为何要使用重定向
当屏幕输出的信息很重要,而且希望保存重要的信息时
后台执行中的程序,不希望他干扰屏幕正常的输出结果时
系统的例行命令, 例如定时任务的执行结果,希望他可以存下来时
一些执行命令,我们已经知道他可能出现错误信息, 想将他直接丢弃时
错误日志与标准正确日志需要分别输出至不同的文件
1.1 标准输入与输出 执行一个shell程序时通常会自动打开三个标准文件
1.1.1 标准文件描述符
文件描述符
名称
说明
默认设备
0
STDIN
标准输入
键盘
1
STDOUT
标准输出
屏幕
2
STDERR
错误输出
屏幕
3+
filename
其他文件
文件
说明 :
标准输入(STDIN,文件描述符为0) : 通常对应终端的键盘,也可从其他文件或命令或者文件内容中输入
标准输出(STDOUT,文件描述符为1) : 默认输出到屏幕
错误输出(STDERR,文件描述符为2) : 默认输出到屏幕
文件名称(filename,文件描述符为3+) : 其他打开的文件
进程将从标准输入中得到数据,将正常输出打印至屏幕终端,将错误的输出信息也打印至屏幕终端。
进程使用文件描述符(file descriptors)来管理打开的文件
1.1.2 cat命令示例 以cat命令为例, cat命令的功能是从命令行给出的文件中读取数据,并将这些数据直接送到标准输出。
1 2 3 4 5 6 7 8 9 10 [root@liyanzhao ~] [root@liyanzhao ~] hello hello ^C
1.1.3 输入输出过程检测 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [root@liyanzhao ~] [root@liyanzhao ~] PID TTY TIME CMD 5848 pts/1 00:00:00 bash 6885 pts/1 00:00:00 tail 6888 pts/1 00:00:00 ps [root@liyanzhao ~] total 0 lrwx------ 1 root root 64 Dec 3 06:57 0 -> /dev/pts/1 lrwx------ 1 root root 64 Dec 3 06:57 1 -> /dev/pts/1 lrwx------ 1 root root 64 Dec 3 06:56 2 -> /dev/pts/1 lr-x------ 1 root root 64 Dec 3 06:57 3 -> /etc/passwd lr-x------ 1 root root 64 Dec 3 06:57 4 -> inotify
1.1.4 Linux查看标准输入输出设备 1 2 3 4 5 [root@liyanzhao ~] lrwxrwxrwx 1 root root 15 Dec 2 22:30 /dev/stderr -> /proc/self/fd/2 lrwxrwxrwx 1 root root 15 Dec 2 22:30 /dev/stdin -> /proc/self/fd/0 lrwxrwxrwx 1 root root 15 Dec 2 22:30 /dev/stdout -> /proc/self/fd/1
1.2 输出重定向 重定向: 改变标准输入、标准输出的方向的就是重定向
1.2.1 重定向符号
符号
说明
示例
>
标准覆盖输出重定向
command > file
>>
标准追加输出重定向
command >> file
2>
错误覆盖输出重定向
command 2> file
2>>
错误追加输出重定向
command 2>> file
<
输入重定向
command < file
&>
正确和错误都重定向到同一文件
command &> file
2>&1
错误输出重定向到标准输出
command > file 2>&1
1.2.2 案例1: 标准输出重定向(覆盖) 1 2 3 4 5 [root@liyanzhao ~] [root@liyanzhao ~]
1.2.3 案例2: 标准输出重定向(追加) 1 2 [liyanzhao@liyanzhao ~]$ echo "This is network conf" >> if
1.2.4 案例3: 错误输出重定向 1 2 3 4 5 6 7 8 9 10 [root@liyanzhao ~] [root@liyanzhao ~] [liyanzhao@liyanzhao ~]$ find /etc -name "*.conf" 1>a 2>b [liyanzhao@liyanzhao ~]$ cat a [liyanzhao@liyanzhao ~]$ cat b
1.2.5 案例4: 正确和错误都输入到相同位置 1 2 3 4 5 [liyanzhao@liyanzhao ~]$ find /etc -name "*.conf" &>ab [liyanzhao@liyanzhao ~]$ cat a b > c
1.2.6 案例5: 重定向到相同的位置
1.2.7 案例6: 重定向到空设备/dev/null 1 2 3 4 5 6 7 8 9 [root@liyanzhao ~] [root@liyanzhao ~] [root@liyanzhao ~] [root@liyanzhao ~]
如果/dev/null设备被删除 :
1 2 3 4 5 6 7 [root@liyanzhao ~] [root@liyanzhao ~]
设备号说明 :
MAJOR主设备号 : 相同主设备号表示为同一种设备类型,也可以认为 kernel 使用的是相同的驱动
MINOR从设备号 : 在同一类型设备中的一个序号
1.2.8 案例7: 脚本中使用重定向 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [root@liyanzhao ~] ping -c1 192.168.69.113 if [ $? -eq 0 ];then echo "192.168.69.113 is up." else echo "192.168.69.113 is down." fi [root@liyanzhao ~] [root@liyanzhao ~] [root@liyanzhao ~] ping -c1 192.168.69.113 &>/dev/null if [ $? -eq 0 ];then echo "192.168.69.113 is up." else echo "192.168.69.113 is down." fi
1.2.9 案例8: 脚本中使用重定向(分别记录) 1 2 3 4 5 6 7 8 9 10 [root@liyanzhao ~] ping -c1 192.168.69.113 &>/dev/null if [ $? -eq 0 ];then echo "192.168.69.113 is up." >>up.txt else echo "192.168.69.113 is down." >>down.txt fi [root@liyanzhao ~] [root@liyanzhao ~]
1.3 输入重定向 标准输入: < 等价 0<
1.3.1 案例1: mail命令 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [root@liyanzhao ~] Subject: hello 1111 2222 3333 . EOT [root@liyanzhao ~] [root@liyanzhao ~] [root@liyanzhao ~]
1.3.2 案例2: grep命令 1 2 3 4 5 6 7 8 [root@liyanzhao ~] xxx xxx [root@liyanzhao ~] root:x:0:0:root:/root:/bin/bash
1.3.3 案例3: dd命令 1 2 3 4 5 [root@liyanzhao ~] [root@liyanzhao ~]
1.3.4 案例4: MySQL导入
1.3.5 案例5: 利用重定向建立多行的文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 [root@liyanzhao ~] [root@liyanzhao ~] 111 [root@liyanzhao ~] 111 222 333 ^D [root@liyanzhao ~] aaa bbb ccc ^D
1.3.6 案例6: 利用重定向建立多行的文件(脚本) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 [root@liyanzhao ~] cat >file200.txt <<EOF 111 222 333 yyy ccc EOF [root@liyanzhao ~] cat <<-EOF +------------------- --- ---- --- ---- --- --- ---- --- --+ || | ====================== | | 虚拟机基本管理 v5.0 | | by liyanzhao | | ====================== | | 1. 安装 KVM | | 2. 安装或重置 CentOS-6.9 | | 3. 安装或重置 CentOS-7.4 | | 5. 安装或重置 Windows-7 | | 6. 删除所有虚拟机 | | q. 退出管理程序 | +------------------- --- ---- --- ---- --- --- ---- --- --+ EOF
1.3.7 案例7: 两条命令同时重定向 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 [root@liyanzhao ~] [root@liyanzhao ~] [root@liyanzhao ~] [root@liyanzhao ~] [1] 6378 [root@liyanzhao ~] [root@liyanzhao ~] [1]+ 运行中 ( while :; do date ; sleep 2; done ) &>/date.txt &
扩展点: subshell
1 2 3 4 5 6 7 [root@liyanzhao ~] [root@liyanzhao ~]
1.4 进程管道技术 管道操作符号 | 连接左右两个命令, 将左侧的命令的标准输出, 交给右侧命令的标准输入
格式 : cmd1 | cmd2 […|cmdn]
1.4.1 案例1: 将/etc/passwd中的用户按UID大小排序 1 2 3 4 5 6 7 8 [root@liyanzhao ~] [root@liyanzhao ~] [root@liyanzhao ~]
1.4.2 案例2: 统计当前/etc/passwd中用户使用的shell类型 1 2 3 4 5 6 7 8 9 10 11 12 13 [root@liyanzhao ~] [root@liyanzhao ~] [root@liyanzhao ~] [root@liyanzhao ~]
1.4.3 案例3: 统计出最占CPU的5个进程
1.4.4 案例4: 统计网站的访问情况 top 20 1 2 3 4 5 6 7 8 9 10 11 12 [root@liyanzhao ~] [root@liyanzhao ~] [root@liyanzhao ~] [root@liyanzhao ~] [root@liyanzhao ~]
1.4.5 案例5: 打印当前所有IP 1 2 3 [root@liyanzhao ~] 127.0.0.1 192.168.69.112
1.4.6 案例6: 打印根分区已用空间的百分比(仅打印数字)
1.4.1 tee管道技术 tee命令可以将数据同时输出到标准输出和文件。
tee命令说明 1 2 3 4 5 6 7 8 9 10 11 [root@liyanzhao ~] 127.0.0.1 192.168.69.112 192.168.122.1 [root@liyanzhao ~] inet 127.0.0.1/8 scope host lo inet 192.168.69.112/24 brd 192.168.69.255 scope global ens32 inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
重定向与tee区别 1 2 3 4 5 [root@liyanzhao ~] [root@liyanzhao ~]
tee常用选项
选项
功能
示例
-a
追加模式
command | tee -a file
-i
忽略中断信号
command | tee -i file
tee应用场景 1 2 3 4 5 6 7 8 tail -f /var/log/messages |tee log_backup.txtcommand |tee file1.txt file2.txtcommand |tee -a log.txt
1.4.2 参数传递xargs xargs将参数列表转换成小块分段传递给其他命令
xargs说明
读入stdin的数据转换为参数添加至命令后面
让一些不支持管道的命令可以使用管道
管道命令符能让大家能进一步掌握命令之间的搭配使用方法,进一步提高命令输出值的处理效率
xargs基本使用 1 2 3 4 5 6 7 8 9 10 11 12 13 [root@liyanzhao ~] 33 [root@liyanzhao ~] lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin [root@liyanzhao~] -rw-r--r-- 1 root root 4653 Dec 2 15:54 passwd -rw-r--r--. 1 root root 4606 Dec 2 15:54 passwd- -rw-r--r--. 1 root root 1454 Sep 23 2014 passwd.OLD
xargs示例 示例1: 管道和标准输出以及标准错误输出 1 2 3 4 5 find /etc/ -name "p*" |grep passwd find /etc/ -name "p*" |grep passwd > a find /etc/ -name "p*" |grep passwd > b find /etc/ -name "p*" |grep passwd &> ab
示例2: 使用xargs传递参数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 [root@liyanzhao ~] -rwxr-xr-x 1 root root 54080 Nov 5 2016 /usr/bin/cat [root@liyanzhao ~] [root@liyanzhao ~] [root@liyanzhao ~]
xargs常用选项
选项
功能
示例
-I {}
指定替换字符串
ls | xargs -I {} cp {} /tmp
-n
每次传递的参数个数
ls | xargs -n 2
-d
指定分隔符
echo "a,b,c" | xargs -d ,
-p
交互式确认
ls | xargs -p rm
-t
显示执行的命令
ls | xargs -t rm
xargs实战案例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 find /tmp -name "*.tmp" |xargs rm -f find /etc -name "*.conf" |xargs -I {} cp {} /backup/ find /var/log -type f |xargs chmod 644 find /etc -name "*.conf" |xargs wc -l find /etc -name "*.conf" |xargs grep "root"
2. 管道注意事项 2.1 注意事项
在管道后面的命令,都不应该在写文件名
1 2 3 4 5 cat file.txt |grep "test" file.txtcat file.txt |grep "test"
在管道中只有标准输出才可以传递下一个命令, 标准错误输出会直接输出终端显示, 建议在使用管道前将标准错误输出重定向
1 2 3 4 5 find /etc -name "*.conf" | grep rc find /etc -name "*.conf" 2>/dev/null | grep rc
有些命令不支持管道技术, 但是可以通过xargs来实现管道传递
1 2 3 4 5 which cat | ls -lwhich cat |xargs ls -l
2.2 管道最佳实践 1 2 3 4 5 6 7 8 9 10 11 find /etc -name "*.conf" 2>/dev/null |grep rc find /tmp -name "*.log" |xargs rm -f find /etc -name "*.conf" |tee conf_list.txt |wc -l ps aux |grep nginx |awk '{print $2}' |xargs kill
3. 重定向与管道综合案例 3.1 案例1: 日志分析 1 2 cat /var/log/access.log |awk '{print $1}' |sort |uniq -c |sort -rn |head -10 |tee top_ip.txt
3.2 案例2: 系统监控脚本 1 2 3 4 5 6 7 8 9 10 11 #!/bin/bash ps aux --sort =-%cpu |head -6 |tee -a cpu_monitor.log ps aux --sort =-%mem |head -6 |tee -a mem_monitor.log df -h |grep -E '^/dev' |awk '{print $5}' |sed 's/%//' |awk '$1 > 80' |tee -a disk_alert.log
3.3 案例3: 批量文件处理 1 2 find /var/log -name "*.log" -type f |xargs -I {} sh -c 'echo "Processing: {}" && tail -100 {} > {}.backup'
3.4 案例4: 数据备份脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #!/bin/bash mysqldump -uroot -p123 database_name > backup.sql 2>error.log gzip backup.sql 2>>error.log mv backup.sql.gz /backup/ 2>>error.logif [ $? -eq 0 ]; then echo "$(date) : Backup successful" >> /var/log/backup.log else echo "$(date) : Backup failed" >> /var/log/backup.log fi
4. 命令总结 4.1 重定向命令
命令
功能
示例
command > file
标准输出重定向(覆盖)
ls > file.txt
command >> file
标准输出重定向(追加)
ls >> file.txt
command 2> file
错误输出重定向
command 2> error.log
command 2>> file
错误输出追加
command 2>> error.log
command &> file
所有输出重定向
command &> all.log
command 2>&1
错误输出重定向到标准输出
command > file 2>&1
command < file
输入重定向
mysql < script.sql
command <<EOF
Here Document
cat <<EOF
4.2 管道命令
命令
功能
示例
command1 | command2
管道
ps aux | grep nginx
command | tee file
同时输出到屏幕和文件
ls | tee file.txt
command | xargs cmd
参数传递
find . -name "*.txt" | xargs rm
command | xargs -I {} cmd {}
使用占位符
ls | xargs -I {} cp {} /tmp
4.3 常用组合 1 2 3 4 5 6 7 8 9 10 11 find /etc -name "*.conf" 2>/dev/null |grep nginx |wc -l cat access.log |awk '{print $1}' |sort |uniq -c |sort -rnfind /var/log -name "*.log" |xargs -I {} tail -100 {} |tee log_summary.txt ps aux |grep -v grep |grep nginx |tee -a nginx_monitor.log