管道shell
主要概念bash
1.用UNIX所谓的“管道”能够把一个进程标准输出流与另外一个进程的标准输入流链接起来。编辑器
2.UNIX中的许多命令被设计为过滤器,从标准输入中读取输入,将输出传送到标准输出。ide
3.bash用“|”在两个命令之间建立管道。ui
进程的输出能够被重定向到终端显示器之外的地方,或者能够让进程从终端键盘之外的地方读取输入。(简而言之:进程的输入能够不从键盘,进程的输出也能够不从显示器)spa
一种最经常使用、最有利的重定向形式是把这两者结合起来,在这种形势下,一个命令输出(标准输出)被直接“用管道输送”到另外一个命令的输入(标准输入)中,从而构成了Linux(和UNIX)所谓的管道(pipe)。设计
当两个命令用管道链接起来时,第一个进程的标准输出流被直接链接到第二个进程的标准输入序列。排序
链接在管道中的全部进程被称为进程组(process group)进程
管道:把前一个命令的输出,做为后一个命令的输入ip
命令1 | 命令2 | 命令3 |···
把passwd中用户名取出来,进行排序
[root@host2 tmp]# cut -d: -f1 /etc/passwd | sort
把passwd中UID号取出来,进行排序
[root@host2 tmp]# cut -d: -f3 /etc/passwd | sort -n
把passwd中用户名取出来,进行排序,而后变成大写
[root@host2 tmp]# cut -d: -f1 /etc/passwd | sort | tr 'a-z' 'A-Z'
【tee】
tee - read from standard input and write to standard output and files
从标准输入读取数据,而且发送至标准输出和文件
即:屏幕输出一份,保存至文件一份
[root@host2 tmp]# echo "Hello Red Squirrel" | tee /tmp/hello.out Hello Red Squirrel [root@host2 tmp]# cat hello.out Hello Red Squirrel
【wc】
wc - print newline, word, and byte counts for each file
只显示文件的行数,不能显示其余信息:
[root@host2 tmp]# wc -l /etc/passwd | cut -d' ' -f1 33
[备注:cut -d的''中间是空格]
题目:
1.统计/usr/bin目录下的文件个数
[root@host2 tmp]# ls /usr/bin/ | wc -l 1434
2.取出当前系统上全部用户的shell,要求每种shell只显示一次,而且按顺序进行显示
[root@host2 tmp]# cut -d: -f7 /etc/passwd | sort -u /bin/bash /bin/sync /sbin/halt /sbin/nologin /sbin/shutdown
3.思考:如何显示/var/log目录下每一个的内容类型?
[root@host2 tmp]# file /var/log/*
4.取出/etc/inittab文件中的第5行
[root@host2 tmp]# head -5 /etc/inittab | tail -1 # System initialization is started by /etc/init/rcS.conf
5.取出/etc/passwd文件中倒数第9个用户的用户名和shell,显示到屏幕上并将其保存至/tmp/users文件中
[root@host2 tmp]# tail -9 /etc/passwd | head -1 | cut -d: -f1,7 | tee /tmp/users haldaemon:/sbin/nologin
6.显示/etc目录下全部以pa开头的文件,并统计其个数
[root@host2 tmp]# ls -d /etc/pa* | wc -l 4
7.不使用文本编辑器,将alias cls=clear 一行内容添加至当前用户的.bashrc文件中
[root@host2 tmp]# echo "alias cls=clear" >> ~/.bashrc