管道和重定向

重定向

file descriptors

0 stdin 标准输入
1 stdout 标准输出
2 stderr 标准错误

查看文件描述符

#ls /proc/$$/fd

重定向案例

[root@tianyun ~]# date 1> date.txt         //
[root@tianyun ~]# date >> date.txt       //
[root@tianyun ~]# ls /home/  /aaaaaaaaa >list.txt    // 
[root@tianyun ~]# ls /home/  /aaaaaaaaa >list.txt 2>error.txt   //
[root@tianyun ~]# ls /home/  /aaaaaaaaa &>list.txt   // 
[root@tianyun ~]# ls /home/  /aaaaaaaaa >list.txt 2>&1   //
[root@tianyun ~]# ls /home/  /aaaaaaaaa >list.txt 2>/dev/null     //

脚本案例

  • 如果/dev/null设备被删除 rm -f /dev/null
    #mknod -m 666 /dev/null c 1 3

  • ping主机

     [root@tianyun ~]# vim ping2.sh 
     ping -c1 10.18.40.100 &>/dev/null
     if [ $? -eq 0 ];then
             echo "10.18.40.100 is up." >>up.txt
     else
             echo "10.18.40.100 is down!" >>down.txt
     fi
    
  • 邮件写入

      [root@tianyun ~]# mail -s "test01" alice < /etc/hosts   //输入重定向,来自于文件
    
  • 文件内容过滤

    [root@tianyun ~]# grep 'root' < /etc/passwd 
    
  • 扩容文件

      [root@tianyun ~]# dd if=/dev/zero of=/file1.txt bs=1M count=2
      [root@tianyun ~]# dd </dev/zero >/file2.txt bs=1M count=20
    
  • mysql导表

      [root@tianyun ~]# mysql -uroot -p123 < bbs.sql
    
  • 脚本编辑

     **1**         
     [root@tianyun ~]# vim create_file.sh
     cat >file200.txt <<EOF
     111
     222
     333
     yyy
     ccc
     EOF
    
     **2**
     [root@tianyun ~]# cat create_file.sh 
     cat <<-EOF
     111
     222
     333
     yyy
     ccc
     EOF
    

进程管道

案例1:将/etc/passwd中的用户按UID大小排序

[root@tianyun ~]# sort -t":" -k3 -n /etc/passwd             //以: 分隔,将第三列按字数升序
[root@tianyun ~]# sort -t":" -k3 -n /etc/passwd -r          //逆序
[root@tianyun ~]# sort -t":" -k3 -n /etc/passwd |head
-t  指定字段分隔符--field-separator
-k 指定列
-n 按数值

案例2:统计出最占CPU的5个进程

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

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

思路:取出第七列(shell) | 排序(把相同归类)| 去重

[root@tianyun ~]# awk -F: '{print $7}' /etc/passwd    
[root@tianyun ~]# awk -F: '{print $7}' /etc/passwd |sort   
[root@tianyun ~]# awk -F: '{print $7}' /etc/passwd |sort |uniq   
[root@tianyun ~]# awk -F: '{print $7}' /etc/passwd |sort |uniq -c  

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

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

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

案例5: 打印当前所有IP

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

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

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

TEE管道案例

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

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

[root@tianyun ~]# date |tee date.txt

[root@tianyun ~]# top -d 1 -b -n 1 |tee top.txt

参数传递

xargs: ls cp rm....

案例1

[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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,470评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,393评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,577评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,176评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,189评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,155评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,041评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,903评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,319评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,539评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,703评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,417评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,013评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,664评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,818评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,711评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,601评论 2 353

推荐阅读更多精彩内容

  • 文件操作: ls ####查看目录中的文件#### ls -F ####查看目录中的文件#### ls -l ##...
    劍風阅读 514评论 0 1
  • centos7 常用命令管理centos服务器的时候常会对文件进行一些常规操作,除了ftp之外了解在ssh下必要的...
    小线亮亮阅读 1,194评论 0 2
  • Linux常用命令大全(非常全!!!) - 鸿燕藏锋 - 博客园 系统信息 arch 显示机器的处理器架构(1) ...
    Meathill大魔王阅读 355评论 0 1
  • 导读 到目前为止,我们已经大致了解了 zsh 的语法特性,可以写一些功能不复杂的脚本了。但 shell 脚本主要的...
    陌辞寒阅读 1,114评论 1 3
  • 文|恬栀。 夏雨微凉,你可知,我一直都在等你。 小雨轻飘 薄凉的雨丝 勾勒出你浅笑的模样 我撑着伞 在熙攘的人群中...
    恬栀阅读 441评论 2 4