管道及 I/O 重定向
I/O重定向
I/O Redirection
标准输入、标准输出、标准错误
输出重定向及综合案例
输入重定向及结合案例
标准输入、标准输出、标准错误
file descriptors (FD,文件描述符 或 Process I/O channels):
进程使用文件描述符来管理打开的文件
[root@CentOS7 ~]# ls /proc/$$/fd
0 1 2 3 4
0, 1, and 2, known as standard input, standard output, and standard error
输出重定向 (覆盖,追加)
正确输出: 1> 1>> 等价于 > >>
错误输出: 2> 2>>
案例1:输出重定向(覆盖)
[root@CentOS7 ~]# date 1> date.txt
案例2:输出重定向(追加)
[root@CentOS7 ~]# date >> date.txt
案例3:错误输出重定向
[root@CentOS7 ~]# ls /home/ /aaaaaaaaa >list.txt
ls: 无法访问/aaaaaaaaa: 没有那个文件或目录
[root@CentOS7 ~]# ls /home/ /aaaaaaaaa >list.txt 2>error.txt //重定向到不同的位置
案例4: 正确和错误都输入到相同位置
[root@CentOS7 ~]# ls /home/ /aaaaaaaaa &>list.txt //混合输出
案例5: 正确和错误都输入到相同位置
[root@CentOS7 ~]# ls /home/ /aaaaaaaaa >list.txt 2>&1 //重定向到相同的位置
案例6:重定向到空设备/dev/null
[root@CentOS7 ~]# ls /home/ /aaaaaaaaa >list.txt 2>/dev/null //空设备,即将产生的输出丢掉
[root@CentOS7 ~]# ls /home/ /aaaaaaaaa &>/dev/null //空设备,即将产生的输出丢掉
cp /etc/passwd /dev/null ???
cp /etc/passwd /etc/passwd1 2>/dev/null ???
sudo rm /dev/null
sudo mknod -m 666 /dev/null c 1 3
案例7:脚本中使用重定向
# vim ping.sh
#!/usr/bin/bash
ping -c1 172.16.120.254 &>/dev/null
if [ $? -eq 0 ];then
echo "up.."
else
echo "down.."
fi
# bash ping.sh
案例8:脚本中使用重定向
# vim ping2.sh
#!/usr/bin/bash
ping -c1 172.16.120.254 &>/dev/null
if [ $? -eq 0 ];then
echo "172.16.120.254 up.." > /up.txt
else
echo "172.16.120.254 down.." >/down.txt
fi
# bash ping2.sh
>new.txt ???
>/etc/passwd ???
>/etc ???
输入重定向
标准输入: < 等价 0<
案例1:
[root@CentOS7 ~]# mail -s "ssss" alice //没有改变输入的方向,默认键盘
111
222
333
^D
[root@CentOS7 ~]# su - alice
[alice@CentOS7 ~]$ mail
Mail version 8.1 6/6/93. Type ? for help.
"/var/spool/mail/alice": 1 message 1 new
>N 1 root@CentOS7.local Mon Oct 29 14:09 18/657 "ssss"
&
[root@CentOS7 ~]# mail -s "test01" alice < /etc/hosts //输入重定向,来自于文件
案例2:
[root@CentOS7 ~]# grep 'root' //没有改变输入的方向,默认键盘,此时等待输入...
yang sss
sssrootssss..
sssrootssss..
[root@CentOS7 ~]# grep 'root' < /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
案例3:
[root@CentOS7 ~]# dd if=/dev/zero of=/file1.txt bs=1M count=2
[root@CentOS7 ~]# dd </dev/zero >/file2.txt bs=1M count=20
案例4:mysql表结构导入
[root@CentOS7 ~]# mysql -uroot -p123 < bbs.sql
案例5:at
[root@CentOS7 ~]# at now +5 min
at> useradd yang99
at> <EOT>
job 1 at 2015-06-09 11:57
[root@CentOS7 ~]# vim at.txt
sudo useradd yang100
sudo useradd yang102
[root@CentOS7 ~]# at now +2 min < at.txt
job 2 at 2015-06-09 11:55
sudo: sorry, you must have a tty to run sudo
sudo: sorry, you must have a tty to run sudo
综合案例1: 利用重定向建立多行的文件
[root@CentOS7 ~]# echo "111" > file1.txt
[root@CentOS7 ~]# cat file1.txt
111
[root@CentOS7 ~]# cat >file2.txt
111
222
333
444
^D
[root@CentOS7 ~]# cat file2.txt
请问:file2.txt有几行?
[root@CentOS7 ~]# cat >>file3.txt
aaa
bbb
ccc
ddd
^D
[root@CentOS7 ~]# cat file3.txt
请问:file3.txt有几行?
[root@CentOS7 ~]# cat >file4 <<EOF
> 111
> 222
> 333
> EOF
[root@CentOS7 ~]# cat file4
111
222
333
综合案例2: 脚本中利用重定向打印消息
[root@CentOS7 ~]# vim yang.sh
#!/usr/bin/bash
cat <<-EOF
+------------------------------------------------+
| |
| ====================== |
| 虚拟机基本管理centos |
| by CentOS7 |
| ====================== |
| 1. 安装虚拟机 |
| 2. 重置所有Linux虚拟机 |
| 3. 重置Windows虚拟机 |
| 4. 重置Windows虚拟机 [完全] |
| 5. 重置指定的虚拟机 |
| q. 退出管理程序 |
| |
+------------------------------------------------+
EOF
综合案例3
[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) &>date.txt
[root@CentOS7 ~]# (while :; do date; sleep 2; done) &>date.txt &
[1] 6595
[root@CentOS7 ~]# tailf date.txt
Tue Apr 12 22:04:32 CST 2016
Tue Apr 12 22:04:34 CST 2016
Tue Apr 12 22:04:36 CST 2016
Tue Apr 12 22:04:38 CST 2016
Tue Apr 12 22:04:40 CST 2016
Tue Apr 12 22:04:42 CST 2016
Tue Apr 12 22:04:44 CST 2016
Tue Apr 12 22:04:46 CST 2016
Tue Apr 12 22:04:48 CST 2016
[root@CentOS7 ~]# jobs
[1]+ Running ( while :; do
date; sleep 2;
done ) &>date.txt &
[root@CentOS7 ~]# kill %1
[root@CentOS7 ~]# jobs
[root@CentOS7 ~]# (./configure && make && make install) &>/dev/null
扩展点:subshell
[root@CentOS7 ~]# (umask 777; touch file8888)
[root@CentOS7 ~]#
[root@CentOS7 ~]# ll file8888
---------- 1 root root 0 Apr 12 22:11 file8888
[root@CentOS7 ~]#
[root@CentOS7 ~]# umask
0022
进程管道 Piping
• Use redirection characters to control output to files.
• Use piping to control output to other programs.
files: > 2> file1.txt /dev/pts/2 /dev/tty1 /dev/null /dev/sda
programs: |
进程管道
用法:command1 | command2 |command3 |...
[root@CentOS7 ~]# ll /dev/ |less
[root@CentOS7 ~]# ps aux |grep 'sshd'
[root@CentOS7 ~]# rpm -qa |grep 'httpd' //查询所有安装的软件包,过滤包含httpd的包
[root@CentOS7 ~]# yum list |grep 'httpd'
案例1:将/etc/passwd中的用户按UID大小排序
[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
-t 指定字段分隔符--field-separator
-k 指定列
-n 按数值
案例2:统计出最占CPU的5个进程
[root@CentOS7 ~]# ps aux --sort=-%cpu |head -6
案例3:统计当前/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
131 /bin/bash
1 /bin/sync
1 /sbin/halt
63 /sbin/nologin
1 /sbin/shutdown
-F: 指定字段分隔符
$7 第七个字段
案例4: 统计网站的访问情况 top 20
思路: 打印所有访问的连接 | 过滤访问网站的连接 | 打印用户的IP | 排序 | 去重
[root@CentOS7 ~]# ss -an |grep :80 |awk -F":" '{print $8}' |sort |uniq -c
4334 192.168.0.66
1338 192.168.10.11
1482 192.168.10.125
44 192.168.10.183
3035 192.168.10.213
375 192.168.10.35
362 192.168.10.39
[root@CentOS7 ~]# ss -an |grep :80 |awk -F":" '{print $8}' |sort |uniq -c |sort -k1 -rn |head -n 20
案例5: 打印当前所有IP
[root@CentOS7 ~]# ip addr |grep 'inet ' |awk '{print $2}' |awk -F"/" '{print $1}'
127.0.0.1
192.168.2.115
案例6:打印根分区已用空间的百分比(仅打印数字)
[root@CentOS7 ~]# df -P |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
172.16.60.1
[root@CentOS7 ~]# cat ip.txt
inet 127.0.0.1/8 scope host lo
inet 172.16.60.1/24 brd 172.16.60.255 scope global eth0
[root@CentOS7 ~]# ip addr |grep 'inet ' |tee -a ip.txt |awk -F"/" '{print $1}' |awk '{print $2}'
127.0.0.1
172.16.60.1
[root@CentOS7 ~]# date |tee date.txt
Sat Mar 11 10:22:31 CST 2017
参数传递 Xargs
awk sed grep sort uniq less more xargs
xargs: ls cp rm
案例1
[root@localhost ~]# touch /home/file{1..5}
[root@localhost ~]# vim files.txt
/home/file1
/home/file2
/home/file3
/home/file4
/home/file5
[root@localhost ~]# cat files.txt |ls -l
[root@localhost ~]# cat files.txt |rm -rvf
cont.
[root@localhost ~]# cat files.txt |xargs ls -l
-rw-r--r--. 1 root root 0 Mar 11 10:35 /home/file1
-rw-r--r--. 1 root root 0 Mar 11 10:35 /home/file2
-rw-r--r--. 1 root root 0 Mar 11 10:35 /home/file4
-rw-r--r--. 1 root root 0 Mar 11 10:35 /home/file5
[root@localhost ~]# cat files.txt |xargs rm -rvf
removed ‘/home/file1’
removed ‘/home/file2’
removed ‘/home/file4’
removed ‘/home/file5’
案例2
[root@localhost ~]# touch /home/file{1..5}
[root@localhost ~]# cat files.txt |xargs -I {} ls -l {}
-rw-r--r--. 1 root root 0 Mar 11 10:40 /home/file1
-rw-r--r--. 1 root root 0 Mar 11 10:40 /home/file2
-rw-r--r--. 1 root root 0 Mar 11 10:40 /home/file4
-rw-r--r--. 1 root root 0 Mar 11 10:40 /home/file5
[root@localhost ~]# cat files.txt |xargs -I {} cp -rvf {} /tmp
‘/home/file1’ -> ‘/tmp/file1’
‘/home/file2’ -> ‘/tmp/file2’
‘/home/file4’ -> ‘/tmp/file4’
‘/home/file5’ -> ‘/tmp/file5’
[root@localhost ~]# cat files.txt |xargs -I YANG cp -rvf YANG /var/tmp
‘/home/file1’ -> ‘/var/tmp/file1’
‘/home/file2’ -> ‘/var/tmp/file2’
‘/home/file4’ -> ‘/var/tmp/file4’
‘/home/file5’ -> ‘/var/tmp/file5’
案例3
[root@localhost ~]# find /etc -iname "*ifcfg*" |xargs -I {} cp -rf {} /tmp
管道及 I/O 重定向的更多相关文章
- linux管道(|)与重定向(<>)的异同
共同点:管道和重定向都改变程序的标准输入或者标准输出 区别: 管道(|)两边都是程序(命令),而重定向(<>)只有左边是程序(命令).即是,管道通过两个子进程来改变两边命令的输入或输出,重 ...
- 管道及I/O重定向
管道及IO 重定向 > < >> <<计算机组成: 运算器,控制器:CPU 存储器:RAM 输入/输出设备 I/O 程序: 指令和数据 控制器:指令 运算器: 存储 ...
- Linux管道及I/O重定向
I/O: 系统设定 默认输入设备:标准输入,STDIN,0 默认输出设备:标准输出,STDOUT,1 标准错误输出:STDERR,2 属于不同的数据流 标准输入:键盘 标准输出和错误输出:显示器 I/ ...
- Linux管道及重定向
Linux管道及重定向 对shell有一定了解的人都知道,管道和重定向是 Linux 中非常实用的 IPC 机制.在shell中,我们通常使用符合'|'来表示管道,符号'>'和'<'表示重 ...
- 重定向-管道技术-xargs命令详解
重定向 什么是重定向? 将原本要输出在屏幕的内容,重新定向输出到指定的文件或设备中. 为什么要使用重定向? 1.备份的时候需要知道备份的结果. 2.屏幕上输出信息比较重要的时候需要保存下来. 3.定时 ...
- Unix/Linux进程间通信(二):匿名管道、有名管道 pipe()、mkfifo()
1. 管道概述及相关API应用 1.1 管道相关的关键概念 管道是Linux支持的最初Unix IPC形式之一,具有以下特点: 管道是半双工的,数据只能向一个方向流动:需要双方通信时,需要建立起两个管 ...
- Linux环境进程间通信(一):管道及命名管道
linux下进程间通信的几种主要手段: 管道(Pipe)及有名管道(named pipe):管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允 ...
- Linux中的pipe(管道)与named pipe(FIFO 命名管道)
catalogue . pipe匿名管道 . named pipe(FIFO)有名管道 1. pipe匿名管道 管道是Linux中很重要的一种通信方式,是把一个程序的输出直接连接到另一个程序的输入,常 ...
- Linux 标准输入输出、错误输出、重定向标准输出
再来看看 >& 操作符: 重定向操作符 描述 > 将命令输出写入到文件或设备(如打印机),而不是命令提示符窗口或句柄. < 从文件而不是从键盘或句柄读入命令输入. >& ...
随机推荐
- Spring(Bean)1
Spring支持3种依赖注入的方式 (DI依赖注入)*属性注入 (配置bean set方法注入) <bean id="car" class="spring.bean ...
- 批量更新Linux文件后缀名
#!/bin/bash#Create_Time 2019-08-06#use: small_wei #查找并,批量修改文件后缀 #后缀为 .txt 修改为 .log find /opt -name & ...
- SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".错误的解决方法
1.今天新git下来的项目报错如下: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".SLF4J: ...
- 【Android - 自定义View】之自定义九宫格手势解锁控件
首先来介绍一下这个自定义View: (1)这个自定义View的名称叫做 LockView ,继承自View类: (2)这个自定义View实现了应用中常见的九宫格手势解锁功能,可以用于保证应用安全: ( ...
- [ch04-03] 用神经网络解决线性回归问题
系列博客,原文在笔者所维护的github上:https://aka.ms/beginnerAI, 点击star加星不要吝啬,星越多笔者越努力. 4.3 神经网络法 在梯度下降法中,我们简单讲述了一下神 ...
- 【Java】Java中的final关键字和static
0.概述 final关键字表示是不可变的: 下面分别从属性(字段).方法.类中进行说明: 1.属性(or字段),表示常量 final声明在属性(or字段)中,表示常量,有两种初始化方法,1是在声明时直 ...
- Netty学习——protoc的新手使用流程
Netty学习——protoc的新手使用流程 关于学习的内容笔记,记下来的东西等于又过了一次脑子,记录的更深刻一些. 1. 使用IDEA创建.proto文件,软件会提示你安装相应的语法插件 安装成功之 ...
- mysql的安装及命令
1.先检查系统是否装有mysql rpm -qa | grep mysql 2.下载mysql的repo源 wget http://192.168.130.150/mysql5.7.26/my ...
- luogu P2650 弹幕考察
题意简化:求某个区间在一组区间中覆盖的数量 对于这个问题,我们很容易想到线段树,或者树状数组,但是maxlongint不能让我们这么做 30分思路: 对于m个区间,枚举n个区间判断与它是否重合 但是O ...
- 牛客竞赛-Who killed Cock Robin
Who killed Cock Robin? I, said the Sparrow, With my bow and arrow,I killed Cock Robin. Who saw him d ...