Linux基础之输入输出

第十五章 输入输出

一、重定向概述

 

1.1、什么是重定向?

将原本要输出到屏幕的数据信息,重新定向到指定的文件中。

比如:每天凌晨定时备份数据,希望将备份数据的结果保存到某个文件中。这样第二天通过查看结果文件,就知道昨天备份数据是成功还是失败。

 

1.2、为何要使用重定向?

1)当屏幕输出的信息很重要,而且希望保存重要的信息的时候。

2)后台执行中的程序,不希望它干扰屏幕的正常输出结果的时候。

3)系统的例行命令,比如定时任务的执行结果,希望它可以存下来的时候。

4)一些执行命令,我们已经知道它可能出现错误信息,想将它直接丢弃的时候。

5)执行一个命令,可能报错输出和正确输出并存,类似错误日志与正确日志需要分别输出到不同的文件的时候。

 

1.3、学习重定向的预备知识,标准输入与输出

当运行一个shell程序时通常会自动打开三个标准文件,分别是标准输入、标准输出、错误输出。

 

1.4、标准输入与输出

执行一个shell程序时通常会自动打开三个文件描述符

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

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

 

 

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

#若使用如下命令,会把文件/etc/passwd的内容依次显示到屏幕上
[root@centos7 ~]# cat /etc/passwd

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

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

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

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

#查看运行的进程
[root@centos7 ~]# 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@centos7 ~]# 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>> 将程序输出的错误结果以追加的方式输出到指定文件,不会覆盖原有文件
标准输入重定向 << 将命令中接收输入的途径由默认的键盘更改为指定的文件或命令

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

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

 

 

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

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

 

示例3:错误输出重定向

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


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

 

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

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

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

 

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

#重定向到相同的位置
[root@centos7 ~]# 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

#将错误输出重定向到 ‘黑洞’,正确内容输出到屏幕/dev/pts/x
[root@centos7 ~]# ls /root/ /err 2>/dev/null

#将错误输出重定向到 ‘黑洞’,正确内容输出到1.txt文件中
[root@centos7 ~]# ls /root/ /err >1.txt 2>/dev/null  

 

示例6:脚本中使用重定向

[root@centos7 ~]# vim ping.sh
#!/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
[root@zls ~]# sh ping.sh


#升级版:
用for循环检测255个:
#!/bin/bash

. /etc/init.d/functions

for n in `seq 255`;do

      ping -c1 -W1 "10.0.0.$n" &>/dev/null

      if [ $? -eq 0 ];then
              action "10.0.0.$n" /bin/true
      else
              action "10.0.0.$n" /bin/false
      fi
done

 

三、输入的重定向使用

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

标准输入: < 等价于 0< << 等价于 0<<

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

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

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


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

 

示例2:

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

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

 

示例3:

[root@centos7 ~]# dd if=/dev/zero of=/file1.txt bs=1M count=20
20+0 records in
20+0 records out
20971520 bytes (21 MB) copied, 0.0260574 s, 805 MB/s
   
[root@centos7 ~]# dd </dev/zero >/file2.txt bs=1M count=20
20+0 records in
20+0 records out
20971520 bytes (21 MB) copied, 0.011896 s, 1.8 GB/s

 

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

#mysql 导入数据
[root@centos7 ~]# 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

 


#脚本 script 创建多行文件
[root@centos7 ~]# vim create_file.sh
cat >file200.txt <<EOF
111
222
333
yyy
ccc
EOF

#脚本中打印菜单的一种使用方法
[root@centos7 ~]# vim vm.sh
cat <<EOF
+--------------------------+
| vmware manager           |
+--------------------------+
| by zls                   |
+--------------------------+
| 1. Install KVM           |
| 2. Install or Reset C6.5 |
| 3. Install or Reset C7.4 |
| 5. Instqll or Reset W7   |
| 6. Remove all           |
| q. quit                 |
+--------------------------+
EOF

 

示例6:

[root@centos7 ~]# vim fruit.sh

# !/bin/sh

menu(){
    cat <<EOF
  +------------+
  | 1 | apple |
  +---+--------+
  | 2 | pear   |
  +---+--------+
  | 3 | banana |
  +---+--------+
  | 4 | cherry |
  +---+--------+
EOF
read -p "please input a num: " fruit
}

usage(){
    echo "USAGE:请输入水果编号"
    exit 1
}

color(){
case "$fruit" in
1)
  echo -e "\E[1;31mapple \E[0m"
  ;;
2)
  echo -e "\E[1;20mpear \E[0m"
  ;;
3)
  echo -e "\E[1;33mbanana \E[0m"
  ;;
4)
  echo -e "\E[1;35mcherry \E[0m"
  ;;
*)
  usage
esac
}
menu
color

 

示例7:多条命令重定向

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

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

 

示例8:后台进程重定向

(while :; do date; sleep 2; done) &>/dev/null &

 

扩展知识: subshell

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

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

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

 

四、管道技术 |

1、什么是管道?

管道操作符号 "|" 连接左右两个命令, 将左侧的命令的标准输出, 交给右侧命令的标准输入。

注意:无法传递标准错误输出至后者命令。

 

2、管道流程示意图

 

 

案例1:将/etc/passwd中的UID取出并按大小排序

[root@centos7 ~]# awk -F : '{print $3}' /etc/passwd|sort -n
[root@centos7 ~]# sort -t":" -k3 -n /etc/passwd
[root@centos7 ~]# sort -t":" -k3 -n /etc/passwd -r
[root@centos7 ~]# sort -t":" -k3 -n /etc/passwd |head

 

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

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

 

案例3:统计最占cpu的5个进程

[root@centos7 ~]# ps aux --sort=-%cpu |head -6

 

案例4:统计网站访问量top20

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

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

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

 

案例5:取出cpu已使用的百分比,只显示数字

[root@centos7 ~]# df -h |awk -F '[ %]+' 'NR==2 {print $5}'
4

 

案例6:打印当前所有ip

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

 

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

[root@centos7 ~]# df |grep '/$' |awk '{print $5}' |awk -F"%" '{print $1}'

 

五、管道中的tee管道技术

 

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

[root@zls ~]# cat ip.txt
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的区别

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

 

六、参数传递xargs

有些命令没有标准输入,通过|xargs 将前面命令的标准输出,交给后面命令来处理。

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

将参数列表转换成小块分段传递给其他命令,读入stdin的数据转换为参数添加至命令后面,让一些不支持管道的命令可以使用管道。
[root@centos7 ~]# grep "/sbin/nologin" /etc/passwd | wc -l
33

[root@centos7 ~]# head -5 /etc/passwd|tail -1
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

#使用grep过滤输出信息
[root@centos7 ~]# ls -l /etc |grep pass
-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

//管道和标准输出以及标准错误输出, 使用普通用户执行如下命令
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

注意事项:

1)在管道后面的命令,都不应该再写文件名。

2)在管道中只有标准输出才可以传递下一个命令,标准错误输出会直接输出终端显示,建议在使用管道前将标准错误输出重定向。

例如: find /etc -name "*.conf" 2>/dev/null | grep rc

3)有些命令不支持管道技术,但是可以通过xargs来实现管道传递。

例如: which cat|xargs ls-l
例如: ls |xargs rm -rvf
例如: ls |xargs cp -rvft /tmp/ -> ls | xargs -I {} cp -rvf {} /tmp
例如: ls |xargs mv -t /tmp/ -> ls | xargs -I {} mv {} /tmp

 

posted @ 2019-08-24 23:23  IT大白2019  阅读(1387)  评论(0编辑  收藏  举报