10.Linux输入输出

徐亮伟, 江湖人称标杆徐。多年互联网运维工作经验,曾负责过大规模集群架构自动化运维管理工作。擅长Web集群架构与自动化运维,曾负责国内某大型电商运维工作。
个人博客”徐亮伟架构师之路“累计受益数万人。
笔者Q:552408925
架构师群:471443208

标杆徐LinuxSre专题课程《运维实战项目训练营》

1.重定向概述

1.什么是重定向
将原本要输出到屏幕的数据信息,重新定向到某个指定的文件中。比如:每天凌晨定时备份数据,希望将备份数据的结果保存到某个文件中。这样第二天通过查看文件的内容就知道昨天备份的数据是成功还是失败。

2.为何要使用重定向
1.当屏幕输出的信息很重要,而且希望保存重要的信息时;
2.后台执行中的程序,不希望他干扰屏幕正常的输出结果时;
3.系统的例行命令, 例如定时任务的执行结果,希望可以存下来时;
4.一些执行命令,我们已经知道他可能出现错误信息, 想将他直接丢弃时;
5.错误日志与正确日志需要分别输出至不同的文件保存时;

3.学习重定向的预备知识,标准输入与输出
当运行一个程序时通常会自动打开三个标准文件,分别是标准输入、标准输出、错误输出

名称 文件描述符 作用
标准输入(STDIN) 0 默认是键盘,也可以是文件或其他命令的输出。
标准输出(STDOUT) 1 默认输出到屏幕。
错误输出(STDERR) 2 默认输出到屏幕。
文件名称(filename) 3+

进程将从标准输入中得到数据,将正常输出打印至屏幕终端,将错误的输出信息也打印至屏幕终端。
PS: 进程是使用文件描述符(file descriptors)来管理打开的文件

以 cat 命令为例, cat 命令的功能是从命令行给出的文件中读取数据,并将这些数据直接送到标准输出。若使用如下命令:

#会把文件/etc/passwd的内容输出显示到屏幕上
[root@xuliangwei ~]# cat /etc/passwd

但如果 使用 cat 命令没有跟上输入的文件名,那么cat命令则会通过命令行标准输入中读取数据, 并将其送到标准输出。

[root@xuliangwei ~]# cat
hello   #标准输入
hello   #标准输出
^C
#用户输入的每一行都立刻被cat命令输出到屏幕上。

下面了解一下标准输入输出过程

#持续追踪查看文件内容
[root@xuliangwei ~]# tail -f /etc/passwd
ctrl+z 将进程转到后台

#查看运行的进程
[root@xuliangwei ~]# ps
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

#查看tail命令的pid,6885进程下的文件描述符
[root@xuliangwei ~]# ls -l /proc/6885/fd
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

#Linux查看标准输入输出设备
[root@xuliangwei ~]# ls -l /dev/std*
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

2.输出重定向

输出重定向,改变输出内容的位置。输出重定向有如下几种方式,如表格所示

类型 操作符 用途
标准覆盖输出重定向 > 将程序输出的正确结果输出到指定的文件中,会覆盖文件原有的内容
标准追加输出重定向 >> 将程序输出的正确结果以追加的方式输出到指定文件,不会覆盖原有文件
错误覆盖输出重定向 2> 将程序的错误结果输出到执行的文件中,会覆盖文件原有的内容
错误追加输出重定向 2>> 将程序输出的错误结果以追加的方式输出到指定文件,不会覆盖原有文件
标准输入重定向 << 将命令中接收输入的途径由默认的键盘更改为指定的文件或命令

案例1: 标准输出重定向(每次都会覆盖文件)

#标准输出重定向, 先清空,后写入, 如果文件不存在则创建
[root@xuliangwei ~]# ifconfig eth0 > abc

案例2: 标准输出重定向(会往文件的尾部在添加内容)

#标准追加输出重定向, 向配置文件末尾追加内容
[xuliangwei@xuliangwei ~]$ echo "This is network conf" >> if 

案例3: 错误输出重定向

#正确输出以及错误输出重定向至一个文件
[root@xuliangwei ~]# useradd xuliangwei
[root@xuliangwei ~]# su - xuliangwei

#将标准输出和标准错误输出重定向到不同文件
[xuliangwei@xuliangwei ~]$ find /etc -name "*.conf" 1>a 2>b

案例4: 正确和错误都输入到相同位置

#将标准输出和标准错误输出重定向到同一个文件, 混合输出
[xuliangwei@xuliangwei ~]$ find /etc -name "*.conf" &>ab

#合并两个文件内容至一个文件
[xuliangwei@xuliangwei ~]$ cat a b > c

案例5: 正确和错误都输入到相同位置

#重定向到相同的位置
[root@xuliangwei ~]# ls /root /error >ab  2>&1

案例6: 重定向到空设备/dev/null

#将产生的任何数据放入黑洞设备,则视为丢弃。
[root@xuliangwei ~]# ls /root /error >ab 2>/dev/null
[root@xuliangwei ~]# ls /root /error >ab &>/dev/null

案例7: 脚本中使用重定向 (了解即可)

[root@xuliangwei ~]# vim ping.sh 
ping -c1 10.0.0.1
if [ $? -eq 0 ];then
    echo "10.0.0.1 is up." 
else
    echo "10.0.0.1 is down." 
fi
[root@xuliangwei ~]# chmod +x ping.sh 
[root@xuliangwei ~]# ./ping.sh

#改进后版
[root@xuliangwei ~]# vim ping.sh
ping -c1 10.0.0.1 &>/dev/null
if [ $? -eq 0 ];then
    echo "10.0.0.1 is up." 
else
    echo "10.0.0.1 is down." 
fi

案例8: 脚本中使用重定向 (了解即可)

[root@xuliangwei ~]# vim ping2.sh 
ping -c1 10.0.0.1 &>/dev/null 
if [ $? -eq 0 ];then
    echo "10.0.0.1 is up." >>up.txt 
else
    echo "10.0.0.1 is down." >>down.txt 
fi
[root@xuliangwei ~]# chmod +x ping2.sh 
[root@xuliangwei ~]# ./ping2.sh

3.输入重定向

输入重定向,即原本从键盘等上获得的输入信息,重定向由命令的输出作为输入。< 等价 0<

案例1: 从文件中读入输入的操作

#没有改变输入的方向,默认键盘
[root@xuliangwei ~]# mail alice 
Subject: hello
1111 
2222
3333
.   #结束
EOT

#检查是否收到邮件
[root@xuliangwei ~]# su - alice
[root@xuliangwei ~]# mail

#输入重定向,来自于文件
[root@xuliangwei ~]# mail -s "test01" alice < /etc/hosts

案例2: 无法形容案例,请看实际操作

#没有改变输入的方向,默认键盘,此时等待输入
[root@xuliangwei ~]# grep 'root' 
xxx
xxx

[root@xuliangwei ~]# grep 'root' < /etc/passwd
root:x:0:0:root:/root:/bin/bash 

案例3: 无法形容案例,请看实际操作

[root@xuliangwei ~]# dd if=/dev/zero of=/file1.txt bs=1M count=20
[root@xuliangwei ~]# dd </dev/zero >/file2.txt bs=1M count=20

案例4: mysql如何恢复备份,了解即可,不用关注。

[root@xuliangwei ~]# mysql -uroot -p123 < bbs.sql

案例5: 利用重定向建立多行数据的文件

#手动执行 shell 命令
[root@xuliangwei ~]# echo "111" > file1.txt 
[root@xuliangwei ~]# cat file1.txt
111
[root@xuliangwei ~]# cat >file2.txt
111
222
333
^D

[root@xuliangwei ~]# cat >>file3.txt
aaa
bbb
ccc
^D

案例6: 脚本中打印菜单的一种使用方法。

[root@xuliangwei ~]# vim vm.sh
cat <<-EOF
+------------------- --- ---- --- ---- --- --- ---- --- --+ ||
| ====================== | 
| 虚拟机基本管理 v5.0 |
| by xuliangwei |
| ====================== | 
| 1. 安装 KVM |
| 2. 安装或重置 CentOS-6.9 | 
| 3. 安装或重置 CentOS-7.4 | 
| 5. 安装或重置 Windows-7  | 
| 6. 删除所有虚拟机 |
| q. 退出管理程序 |
+------------------- --- ---- --- ---- --- --- ---- --- --+ 
EOF

案例7: 两条命令同时重定向

[root@xuliangwei ~]# ls; date &>/dev/null
[root@xuliangwei ~]# ls &>/dev/null; date &>/dev/null
[root@xuliangwei ~]# (ls; date) &>/dev/null

#后台执行
[root@xuliangwei ~]# (while :; do date; sleep 2; done) &
[1] 6378
[root@xuliangwei ~]# (while :; do date; sleep 2; done) &>date.txt &
[root@xuliangwei ~]# jobs
[1]+ 运行中 ( while :; do date; sleep 2;
done ) &>/date.txt &

扩展点: subshell 了解即可

[root@xuliangwei ~]# cd /boot; ls

//subshell 中执行
[root@xuliangwei ~]# (cd /boot; ls)

#如果不希望某些命令的执行对当前 shell 环境产生影响,请在subshell中执行

4.进程管道技术

1.什么是管道
管道操作符号 “|” ,主要用来连接左右两个命令, 将左侧的命令的标准输出, 交给右侧命令的标准输入
PS: 无法传递标准错误输出至后者命令

2.管道流程示意图


格式: cmd1 | cmd2 [...|cmdn]

3.管道使用案例
案例1: 将/etc/passwd 中的用户按 UID 大小排序

[root@xuliangwei ~]# sort -t":" -k3 -n /etc/passwd
[root@xuliangwei ~]# sort -t":" -k3 -n /etc/passwd -r
[root@xuliangwei ~]# sort -t":" -k3 -n /etc/passwd |head

案例2: 统计当前/etc/passwd 中用户使用的 shell 类型

#思路:取出第七列(shell) | 排序(把相同归类)| 去重
[root@xuliangwei ~]# awk -F: '{print $7}' /etc/passwd
[root@xuliangwei ~]# awk -F: '{print $7}' /etc/passwd |sort
[root@xuliangwei ~]# awk -F: '{print $7}' /etc/passwd |sort |uniq
[root@xuliangwei ~]# awk -F: '{print $7}' /etc/passwd |sort |uniq -c

案例4: 统计网站的访问情况 top 20

#思路: 打印所有访问的连接 | 过滤访问网站的连接 | 打印用户的 IP | 排序 | 去重

[root@xuliangwei ~]# yum -y install httpd
[root@xuliangwei ~]# systemctl start httpd
[root@xuliangwei ~]# systemctl stop firewalld

[root@xuliangwei ~]# ss -an |grep :80 |awk -F":" '{print $8}' |sort |uniq -c
[root@xuliangwei ~]# ss -an |grep :80 |awk -F":" '{print $8}' |sort |uniq -c |sort -k1 -rn |head -n 20

案例5: 打印当前所有 IP

[root@xuliangwei ~]# ip addr |grep 'inet ' |awk '{print $2}' |awk -F"/" '{print $1}'
127.0.0.1
192.168.69.112

案例6: 打印根分区已用空间的百分比(仅打印数字)

[root@xuliangwei ~]# df |grep '/

PS: 管道命令符能让大家能进一步掌握命令之间的搭配使用方法,进一步提高命令输出值的处理效率。

4.管道中的tee技术

#选项: -a追加
[root@xuliangwei ~]# ip addr |grep 'inet ' |tee ip.txt |awk -F"/" '{print $1}' |awk '{print $2}'
127.0.0.1
10.0.0.100

[root@xuliangwei ~]# cat ip.txt
inet 127.0.0.1/8 scope host lo
inet 10.0.0.100/24 brd 192.168.69.255 scope global ens32

重定向与 tee 有他们在使用过程中有什么区别

[root@xuliangwei ~]# date > date.txt    #直接将内容写入date.txt文件中
[root@xuliangwei ~]# date |tee date.txt #命令执行会输出至屏幕,但会同时保存一份至date.txt文件中

5.xargs参数传递,主要让一些不支持管道的命令可以使用管道技术

# which cat|xargs ls- l
# ls |xargs rm -fv
# ls |xargs cp -rvt /tmp/ -或-> ls | xargs -I {} cp -rv {} /tmp/
# ls |xargs mv -t /tmp/   -或-> ls | xargs -I {}  mv {} /tmp

|awk '{print $5}' |awk -F"%" '{print $1}'

PS: 管道命令符能让大家能进一步掌握命令之间的搭配使用方法,进一步提高命令输出值的处理效率。

4.管道中的tee技术


重定向与 tee 有他们在使用过程中有什么区别


5.xargs参数传递,主要让一些不支持管道的命令可以使用管道技术


上一篇
下一篇