管道及重定向

| 管道符 前面的结果给后面执行

'>>'
<<
标准输入 描述符 0 -------> /proc/ ----> fd 文件描述符位置
lsof -p 4510 查看打开文件的描述符
vim mima.sh
#!/bin/bash
read -p “请你输入用户名称:” username 读用户的标准输入
stty -echo
read -p “请你输入用户密码:” pass
stty echo
echo "$useradd 赶紧把你的银行卡换掉,我已经知道你的密码了,你的密码是:“ $pass ”对不对“
标准正确输出 描述符 1
标准错误输出 描述符 2 3+(进程在执行过程中打开的其他的文件)
find / -size +3G ’1‘ 是正确 ‘2’是错误
只想要正确不要错误# find / -size +3G 2> /dev/null 定向到垃圾桶
正确和错误都输入到相同位置# find / -size +3G &>list.txt //混合输出
都丢到一起# ls find / -size +3G 1> /dev/null 2>&1
&gt;前面什么都不写比表示1
输出重定向(追加)# date >> date.txt
输出重定向(覆盖)# date 1> date.txt
屏幕回显
取消屏幕回显:#stty -echo


文件描述符
file descriptors 简称fd 或 Process I/O channels
进程使用文件描述符来管理打开的文件
[root@wing ~]# ls /proc/$$/fd $$当前的进程号
0 1 2 3 4

  1. 手动创建 /dev/null
    mknod -m 666 /dev/null c 1 3
    管道及重定向
    主设备号相同: 表示为同一种设备类型,也可以认为kernel使用的是相同的驱动
    从设备号:在同一类型设备中的一个序号

普通文件和设备文件:
[root@wing ~]# ll /dev/null /dev/sda1 /etc/hosts
crw-rw-rw- 1 root root 1, 3 8月 1 06:36 /dev/null
brw-rw---- 1 root disk 8, 18月 1 06:36 /dev/sda1
-rw-r--r--. 1 root root 158 6月 7 2013 /etc/hos
案例7:脚本中使用重定向
[root@wing ~]# vim ping1.sh

ping -c1 10.18.40.100
if [ $? -eq 0 ];then
        echo "10.18.40.100 is up."
else
        echo "10.18.40.100 is down!"
fi

[root@wing ~]# vim ping1.sh
[root@wing ~]# chmod +x ping1.sh
[root@wing ~]# ./ping1.sh

[root@wing ~]# vim ping1.sh

ping -c1 10.18.40.100 &>/dev/null
if [ $? -eq 0 ];then
        echo "10.18.40.100 is up."
else
        echo "10.18.40.100 is down!"
fi

案例8:脚本中使用重定向
[root@wing ~]# vim ping2.sh
ping -c1 10.18.40.100&&gt;/dev/null
if [ $? -eq 0 ];then
echo "10.18.40.100 is up."&gt;&gt;up.txt
else
echo "10.18.40.100 is down!" &gt;&gt;down.txt
fi
[root@wing ~]# vim ping2.sh
[root@wing ~]# chmod +x ping1.sh
[root@wing ~]# ./ping2.sh
输入重定向
标准输入: < 等价 0<
案例1:
[root@wing ~]# mail alice //没有改变输入的方向,默认键盘
Subject: hello
1111
2222
3333
.
EOT
[root@wing ~]# su - alice
[alice@wing ~]$ mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/alice": 1 message 1 new
&gt;N 1 root Mon Jul 31 15:16 20/617 "hello"
[root@wing ~]# mail -s "test01" alice < /etc/hosts //输入重定向,来自于文件
案例2:
[root@wing ~]# grep 'root' //没有改变输入的方向,默认键盘,此时等待输入...
yang sss
sssrootssss..
sssrootssss..

[root@wing ~]# grep 'root' < /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

案例3:
[root@wing ~]# dd if=/dev/zero of=/file1.txt bs=1M count=2
[root@wing ~]# dd </dev/zero >/file2.txt bs=1M count=20

案例4:mysql表结构导入
[root@wing ~]# mysql -uroot -p123 < bbs.sql

案例5:at
[root@wing ~]# at now +5 min
at> useradd yang99
at> <EOT>
job 1 at Mon Jul 31 15:29:00 2017
[root@wing ~]# vim at.txt
sudo useradd yang100
sudo useradd yang102
[root@wing ~]# at now +2 min <a.txt
job 2 at Mon Jul 31 15:27:00 2017
综合案例1: 利用重定向建立多行的文件 手动执行shell命令
[root@wing ~]# echo "111" > file1.txt
[root@wing ~]# cat file1.txt
111
[root@wing ~]# cat&gt;file2.txt
111
222
333
444
^D
[root@wing ~]# cat file2.txt
请问:file2.txt有几行?
[root@wing ~]# cat &gt;&gt;file3.txt
aaa
bbb
ccc
ddd
^D
[root@wing ~]# cat file3.txt
请问:file3.txt有几行?
[root@wing ~]# cat >>file4 <<EOF

  • > 111
  • > 222
  • > 333
  • > EOF
    [root@wing ~]# cat file4
    111
    222
    333
    综合案例2: 利用重定向建立多行的文件 脚本script创建多行文件
    [root@wing ~]# vim create_file.sh
    cat >>file200.txt <<EOF
    111
    222
    333
    yyy
    ccc
    EOF
    [root@wing ~]# bash create_file.sh
    [root@wing ~]# cat file200.txt
    111
    222
    333
    yyy
    ccc
    综合案例3: 脚本中利用重定向打印消息
    [root@wing ~]# cat create_file.sh
    cat <<-EOF
    111
    222
    333
    yyy
    ccc
    EOF
    [root@wing ~]# bash create_file.sh
    111
    222
    333
    yyy
    ccc
    [root@wing ~]# vim virtual.sh
    cat <<-EOF
    +------------------------------------------------+

    ======================
    虚拟机基本管理 v4.0
    by wing
    ======================
    1. 安装KVM
    2. 安装或重置CentOS-6.8
    3. 安装或重置CentOS-7.3
    4. 安装或重置RHEL-6.4
    5. 安装或重置Windows-7
    6. 删除所有虚拟机
    q. 退出管理程序

    +------------------------------------------------+
    EOF
    综合案例4
    [root@wing ~]# ls; date &>/dev/null //希望两条命令输出都重定向 ??
    [root@wing ~]# ls &>/dev/null; date &>/dev/null
    [root@wing ~]# (ls; date) &>/dev/null
    [root@wing ~]# (while :; do date; sleep 2; done)& //在后台运行,但输出依然在前台终端
    [1] 6229
    [root@wing ~]# 2017年 08月 01日 星期二 10:12:42 CST
    2017年 08月 01日 星期二 10:12:44 CST
    2017年 08月 01日 星期二 10:12:46 CST
    2017年 08月 01日 星期二 10:12:48 CST
    2017年 08月 01日 星期二 10:12:50 CST
    [root@wing ~]# (while :; do date; sleep 2; done) &&gt;date.txt &
    [root@wing ~]# tailf /date.txt
    2017年 08月 01日 星期二 10:15:29 CST
    2017年 08月 01日 星期二 10:15:31 CST
    2017年 08月 01日 星期二 10:15:33 CST
    2017年 08月 01日 星期二 10:15:35 CST
    2017年 08月 01日 星期二 10:15:37 CST
    2017年 08月 01日 星期二 10:15:39 CST
    2017年 08月 01日 星期二 10:15:41 CST
    [root@wing ~]# jobs
    [1]+ 运行中 ( while :; do
    date; sleep 2;
    done ) &>/date.txt &
    [root@wing ~]# kill %1
    [root@wing ~]# jobs
    后面课程学习安装源码软件时:
    [root@wing ~]# (./configure && make && make install) &>/dev/null
    扩展点:subshell
    ==当前shell中执行==
    [root@wing ~]# cd /boot; ls
    config-3.10.0-514.el7.x86_64
    efi
    grub
    grub2
    initramfs-0-rescue-a024cb8d031d445580a7b5aaf92a9ca0.img
    initramfs-3.10.0-514.el7.x86_64.img
    initrd-plymouth.img
    symvers-3.10.0-514.el7.x86_64.gz
    System.map-3.10.0-514.el7.x86_64
    vmlinuz-0-rescue-a024cb8d031d445580a7b5aaf92a9ca0
    vmlinuz-3.10.0-514.el7.x86_64
    [root@wing boot]#
    ==在subshell中执行==
    [root@wing boot]# cd
    [root@wing ~]# (cd /boot; ls)
    config-3.10.0-514.el7.x86_64
    efi
    grub
    grub2
    initramfs-0-rescue-a024cb8d031d445580a7b5aaf92a9ca0.img
    initramfs-3.10.0-514.el7.x86_64.img
    initrd-plymouth.img
    symvers-3.10.0-514.el7.x86_64.gz
    System.map-3.10.0-514.el7.x86_64
    vmlinuz-0-rescue-a024cb8d031d445580a7b5aaf92a9ca0
    vmlinuz-3.10.0-514.el7.x86_64
    [root@wing ~]#
    如果不希望某些命令的执行对当前shell环境产生影响,请在subshell中执行!
    [root@wing ~]# (umask 777; touch file8888)

[root@wing ~]# ll file8888
---------- 1 root root 0 Apr 12 22:11 file8888

[root@wing ~]# umask
0022


参数传递 Xargs
awk sed grep sort uniq less more xargs
xargs: ls cp rm
cat a.txt
/etc/passwd
cat a.txt | xargs ls
cat a.txt | xargs -i cp {} /tmp(把前面文件的东西一个一个给{})
cat a.txt | xargs -I {} cp {} /tmp (把前面文件里的文件-I 给{} ,再给{})
cat files.txt |xargs ls -l (前面文件放到后面不需要加路径就不用{})
find -exec cp {} /etc 全部 find | xargs cp
用一台虚拟机的两个终端
echo ' ' /dev/ps1
a.txt 里面是目录的话
cat a.txt | xargs cp -r /tmp
例子:
#touch ac.txt
#vim ac.txt
/root/a.txt
#cat ac.txt | xargs -i cp {} /tmp
#cd /tmp
#ls
#cat a.txt

进程管道 piping
• Use redirection characters to control output to files.
• Use piping to control output toother programs.

files:> 2> file1.txt /dev/pts/2 /dev/tty1 /dev/null /dev/sda
programs:|

进程管道
用法:command1 | command2 |command3 |...

[root@wing ~]# ll /dev/ |less
[root@wing ~]# ps aux |grep 'sshd'
[root@wing ~]# rpm -qa |grep 'httpd' //查询所有安装的软件包,过滤包含httpd的包
[root@wing ~]# yum list |grep 'httpd'

案例1:将/etc/passwd中的用户按UID大小排序
[root@wing ~]# sort -t":" -k3 -n /etc/passwd //以: 分隔,将第三列按字数升序
[root@wing ~]# sort -t":" -k3 -n /etc/passwd -r //逆序
[root@wing ~]# sort -t":" -k3 -n /etc/passwd |head
-t 指定字段分隔符--field-separator
-k 指定列
-n 按数值

案例2:统计出最占CPU的5个进程
[root@wing ~]# ps aux --sort=-%cpu |head -6

案例3:统计当前/etc/passwd中用户使用的shell类型
思路:取出第七列(shell)|排序(把相同归类)|去重
[root@wing ~]# awk -F: '{print $7}' /etc/passwd
[root@wing ~]# awk -F: '{print $7}' /etc/passwd |sort
[root@wing ~]# awk -F: '{print $7}' /etc/passwd |sort |uniq
[root@wing ~]# 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@wing ~]# yum -y install httpd
[root@wing ~]# systemctl start httpd
[root@wing ~]# systemctl stop firewalld

[root@wing ~]# 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@wing ~]# ss -an |grep :80 |awk -F":" '{print $8}' |sort |uniq -c |sort -k1 -rn |head -n 20

案例5: 打印当前所有IP
[root@wing ~]# ip addr |grep 'inet ' |awk '{print $2}' |awk -F"/" '{print $1}'
127.0.0.1
192.168.2.115

案例6:打印根分区已用空间的百分比(仅打印数字)
[root@wing ~]# df -P |grep '/$' |awk '{print $5}' |awk -F"%" '{print $1}'

猜你喜欢

转载自blog.51cto.com/13767724/2120620