重定向
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