Linux运维学习之路(8)管道及重定向
来源:互联网 发布:程序员周末兼职 编辑:程序博客网 时间:2024/06/03 20:26
一、文件描述符
文件描述符(fd):文件描述符是一个非负整数,在打开现存文件或新建文件时,内核会返回一个文件描述符,读写文件也需要使用文件描述符来访问文件。它也是内核为每个进程维护该进程打开的文件记录表。文件描述符只适于Unix、Linux操作系统。
二、标准输入、标准输出、标准错误
三、重定向符号
案例一:输出重定向(覆盖)
# date > date.txt
可以看到原来文件中的内容被覆盖了。
案例二:输出重定向(追加)
# echo “world” >> date.txt
可以看到原来的文件并没有被覆盖,而是在末尾又添加了一行。
案例三:错误输出重定向
# ls /nginx001 /home > right.txt 2> error.txt (覆盖)
# ls /home/jack /root >>right.txt 2>>error.txt (追加)
案例四:正确错误都输出到相同位置
方式一
# ping -c1 -W1 192.168.1.103 &>file01.txt# ls /home /king &>filefile02.txt
方式二
# ls /hello /home >file03.txt 2>&1
案例五:重定向到空设备(/dev/null)
前面文件管理章节我们提到过/dev/null空设备文件,它的作用类似于空间黑洞,可以将输出重定向到它里面,却不会给你任何回复。
# ping -c1 -W1 127.0.0.1# ping -c1 -W1 127.0.0.1 &>/dev/null# echo “hello” &>/dev/null
案例六:脚本中使用重定向
# vim pingTest01.ship= 192.168.144.132ping -c1 -W1 $ipif [ $? -eq 0 ];then echo "$ip is up."else echo "$ip is down!"fi# bash pingTest01.sh改进,把不必要的输出放到黑洞(/dev/null)# vim pingTest02.ship= 192.168.144.132ping -c1 -W1 $ip &>/dev/nullif [ $? -eq 0 ];then echo "$ip is up."else echo "$ip is down!"fi# bash pingTest02.sh改进,都不输出,把结果保存下来# vim pingTest03.ship= 192.168.144.132ping -c1 -W1 $ip &>/dev/nullif [ $? -eq 0 ];then echo "$ip is up." >>up.txtelse echo "$ip is down!" >>down.txtfi# bash pingTest03.sh
案例七:输入重定向的使用
示例一:邮件输入重定向
1)邮件命令:mail
选项:
-s 指定邮件的主题
例1
# mail -s “object” 14625@qq.com 主题 接收者
例2
使用管道发送邮件
# echo “hello $USER,welcome to Linux Mail\!” |mail -s “welcome” root# mail
命令:1 查看邮件1d 1 删除邮件1q 退出? 帮助
例3
使用文件输入重定向进行邮件发送
# echo “hello world!,I am Ccjedweat\!” >hello.txt# mail -s “hello” root <hello.txt
示例二:输入重定向之while循环读取# vim redirectTest.shwhile read line; do echo “$line”done </etc/passwd# bash redirectTest.sh
示例三:mysql表结构导入# mysql -uroot -p123 < user.sql
示例四:脚本中利用重定向输出多行文件# cat <<-EOF1111111222222333333EOF
输出文件内容直到遇到EOF,<<-EOF中的-会跳过下面内容中的空格或tab键,省略的话就不能跳过空格或tab键的影响。
示例五:利用重定向建立多行的文件# cat >test.txt <<-EOF大器何必晚成,赢在坚持!EOF
扩展点:subshell
在当前shell中执行
# cd /boot; ls# pwd
可以看到当前工作目录改变了
在subshell(子shell)中执行
# cd# (cd /boot; ls)# pwd
可以看到当前工作目录没有改变
如果不希望某些命令的执行对当前shell环境产生影响,请在subshell中执行!
四、管道
1.简介
管道是一种最基本的IPC机制,作用于有血缘关系的进程之间,完成数据传递。它可以将上一个命令的输出作为下一个命令的输入。在脚本编程中,管道的运用是很普遍的,也是非常重要的。管道有匿名管道和命名管道,这里主要介绍匿名管道。
2.语法结构
进程管道
用法:command1 | command2 |command3 |…
# ll /dev/ |file01# ps aux |grep 'sshd'# rpm -qa |grep 'httpd' //查询所有安装的软件包,过滤包含httpd的包# yum list |grep 'httpd'
案例1:将/etc/passwd中的用户按UID大小排序# sort -t":" -k3 -n /etc/passwd //以:分隔,将第三列按字数升序# sort -t":" -k3 -n /etc/passwd -r //逆序# sort -t":" -k3 -n /etc/passwd |head-t 指定字段分隔符-k 指定列-r 反向排序-n 按数值
案例2:统计出最占CPU的5个进程# ps aux --sort=-%cpu |head -5
案例3:统计当前/etc/passwd中用户使用的shell类型思路:取出第七列(shell) | 排序(把相同归类)| 去重# awk -F”:” '{print $7}' /etc/passwd# awk -F”:” '{print $7}' /etc/passwd |sort# awk -F”:” '{print $7}' /etc/passwd |sort |uniq # awk -F”:” '{print $7}' /etc/passwd |sort |uniq -c-F指定字段分隔符$7 第七个字段
案例4: 统计网站的访问情况 top 20思路:打印所有访问的连接|过滤访问网站的连接|打印用户的IP|排序|去重# yum -y install httpd# systemctl start httpd# systemctl stop firewalld# ss -an |grep :80 |awk -F":" '{print $8}' |sort |uniq -c# ss -an |grep :80 |awk -F":" '{print $8}' |sort |uniq -c |sort -k1 -rn |head -n 20
案例5: 打印当前所有IP# ip addr |grep 'inet ' |awk '{print $2}' |awk -F"/" '{print $1}'案例6:打印根分区已用空间的百分比(仅打印数字)# df -P |grep '/$' |awk '{print $5}' |awk -F"%" '{print $1}'
3.tee管道
想要记录管道的某一部分的管道数据,可以在其后面加一个tee管道,它的作用有点类似于输出重定向,可以把tee管道看成是自来水管的3分支,互不影响,也就是说去掉或加上tee管道对匿名管道命令没有任何影响,它只是用来接收记录某个匿名管道的输出。
# grep '^root' >user_info01.txt# gerp '^root' |tee user_info02.txt# ip addr |grep 'inet ' |tee ip.txt |awk -F"/" '{print $1}' |awk '{print $2}'以追加的方式写入ip.txt文件# ip addr |grep 'inet ' |tee -a ip.txt |awk -F"/" '{print $1}' |awk '{print $2}'
在这里小编给你们留两个问题,大家借助身边的一切工具可以去了解下?1. 了解匿名管道和命名管道的区别?2. 如何创建命名管道?
五、参数传递Xargs
awk sed grep sort uniq less more xargs可以直接跟管道命令,而ls cp rm不能直接跟管道命令,需要通过xargs命令传递参数才可以。
案例1[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 -rfv[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[root@localhost ~]# cat files.txt |xargs -I CCjetweat cp -rvf CCjetweat /var/tmp
案例3[root@localhost ~]# find /etc -iname "*ifcfg*" |xargs -I {} cp -rf {} /tmp
请多多支持,有错误的地方,欢迎指出,未完待续…
- Linux运维学习之路(8)管道及重定向
- Linux管道及重定向
- Linux管道及重定向
- linux初学(十七)之linux管道及重定向
- linux学习笔记之输出重定向管道符及通配符
- Linux中重定向及管道
- linux重定向及管道使用
- Linux中重定向及管道
- linux中重定向及管道
- Linux中重定向及管道
- linux输入输出重定向及管道
- Linux中重定向及管道
- 【Linux 入门笔记】管道及重定向
- Linux中重定向及管道
- Linux-管道及IO重定向
- Linux管道、重定向及文本处理
- Linux中>、>>、<、<<重定向及管道命令
- linux的管道及重定向
- surefire单元测试 并发 提速
- centos7.3yum安装mongodb3.4
- Maven四
- Codeforces Round #433 (Div. 1, based on Olympiad of Metropolises) C. Boredom
- xml 和 json
- Linux运维学习之路(8)管道及重定向
- 读取/写入文件
- 多线程第二章(2)
- 判断101-200之间有多少个素数,并输出所有素数(python实现)
- 基于loongson(mips)平台的台式机系统安装
- OpenCV中读取YAML文件方法
- 进制转换
- mysql锁隔离机制
- RTP-RTCP协议分析