首页 > 代码库 > linux基础知识-I/O重定向,管道
linux基础知识-I/O重定向,管道
系统设定
默认输出设备:标准输出,STDOUT, 1
默认输入设备:标准输入, STDIN, 0
标准错误输出:STDERR, 2
标准输入:键盘
标准输出和错误输出:显示器
I/O重定向:
Linux:
>: 覆盖输出
[root@localhost ~]# ll /var/ > /tmp/var.out
[root@localhost ~]# cat /tmp/var.out
total 76
drwxr-xr-x. 2 root root 4096 Jun 21 2015 account
drwxr-xr-x. 13 root root 4096 Jun 21 2015 cache
drwxr-xr-x. 2 root root 4096 Jun 21 2015 crash
drwxr-xr-x. 3 root root 4096 Jun 21 2015 db
drwxr-xr-x. 3 root root 4096 Jun 21 2015 empty
drwxr-xr-x. 2 root root 4096 Jun 28 2011 games
#
# /etc/fstab
# Created by anaconda on Sun Jun 21 02:15:00 2015
#
# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=57d85756-7680-4c7c-9125-6ad67dae2c45 / ext4 defaults 1 1
UUID=2622a4b4-ddc9-47a3-aa2b-f06bc9bec085 /boot ext4 defaults 1 2
UUID=33d94759-fa01-4c4f-b4ac-bf3a1fe5e84f swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
>>:追加输出
2>: 重定向错误输出
[root@localhost ~]# ls /varr > /tmp/var2.out
ls: cannot access /varr: No such file or directory
[root@localhost ~]# ls /varr 2> /tmp/var2.out
[root@localhost ~]# cat /tmp/var2.out
ls: cannot access /varr: No such file or directory
[root@localhost ~]#
2>>: 追加方式
&>: 重定向标准输出或错误输出至同一个文件
[root@localhost ~]# ls /var6 &> /tmp/var3.out
[root@localhost ~]# cat /tmp/var3.out
ls: cannot access /var6: No such file or directory
[root@localhost ~]# ls /var &> /tmp/var3.out
[root@localhost ~]# cat /tmp/var3.out
account
cache
crash
db
empty
输入重定向
<:输入重定向
[root@localhost ~]# tr ‘a-z‘ ‘A-Z‘ < /etc/fstab
#
# /ETC/FSTAB
# CREATED BY ANACONDA ON SUN JUN 21 02:15:00 2015
#
# ACCESSIBLE FILESYSTEMS, BY REFERENCE, ARE MAINTAINED UNDER ‘/DEV/DISK‘
# SEE MAN PAGES FSTAB(5), FINDFS(8), MOUNT(8) AND/OR BLKID(8) FOR MORE INFO
#
UUID=57D85756-7680-4C7C-9125-6AD67DAE2C45 / EXT4 DEFAULTS 1 1
UUID=2622A4B4-DDC9-47A3-AA2B-F06BC9BEC085 /BOOT EXT4 DEFAULTS 1 2
UUID=33D94759-FA01-4C4F-B4AC-BF3A1FE5E84F SWAP SWAP DEFAULTS 0 0
TMPFS /DEV/SHM TMPFS DEFAULTS 0 0
DEVPTS /DEV/PTS DEVPTS GID=5,MODE=620 0 0
SYSFS /SYS SYSFS DEFAULTS 0 0
PROC /PROC PROC DEFAULTS 0 0
[root@localhost ~]#
<<:此处是文档
[root@localhost ~]# cat << END
> the first line
> the second line
> END
the first line
the second line
[root@localhost ~]#
从键盘中读入数据,并保存在文档中
cat >> /tmp/myfile.txt << EOF
[root@localhost ~]# cat >> /tmp/myfile.txt << EOF
> the first line
> the second line
> EOF
[root@localhost ~]# cat /tmp/myfile.txt
the first line
the second line
[root@localhost ~]#
管道:前一个命令的输出,作为后一个命令的输入
命令1 | 命令2 | 命令3 | ...
[root@localhost ~]# echo "hello,world" | tr ‘a-z‘ ‘A-Z‘
HELLO,WORLD
[root@localhost ~]#
[root@localhost ~]# echo "redhat" | passwd --stdin hive
Changing password for user hive.
passwd: all authentication tokens updated successfully.
[root@localhost ~]#
同时输出到屏幕和文件中
[root@localhost ~]# echo "hello,world" | tee /tmp/hello.out
hello,world
[root@localhost ~]# cat /tmp/hello.out
hello,world
[root@localhost ~]#
统计文件行数
[root@localhost ~]# wc -l /etc/passwd
32 /etc/passwd
[root@localhost ~]# wc -l /etc/passwd | cut -d‘ ‘ -f1
32
[root@localhost ~]#
练习:
1、统计/usr/bin/目录下的文件个数;
# ls /usr/bin | wc -l
2、取出当前系统上所有用户的shell,要求,每种shell只显示一次,并且按顺序进行显示;
# cut -d: -f7 /etc/passwd | sort -u
3、思考:如何显示/var/log目录下每个文件的内容类型?
4、取出/etc/inittab文件的第6行;
# head -6 /etc/inittab | tail -1
5、取出/etc/passwd文件中倒数第9个用户的用户名和shell,显示到屏幕上并将其保存至/tmp/users文件中;
# tail -9 /etc/passwd | head -1 | cut -d: -f1,7 | tee /tmp/users
6、显示/etc目录下所有以pa开头的文件,并统计其个数;
# ls -d /etc/pa* | wc -l
7、不使用文本编辑器,将alias cls=clear一行内容添加至当前用户的.bashrc文件中;
# echo "alias cls=clear" >> ~/.bashrc
linux基础知识-I/O重定向,管道