《鸟哥的Linux私房菜》学习笔记(6)——管道及IO重定向

一、标准I/O                                                           

标准输入:也可以叫STDIN,用0来标识,通常是键盘

标准输出:也可以叫STDOUT,用1来标识,通常是显示器

标准错误输出:STDERR,用2来标识,通常是显示器

二、I/0重定向                                                        

I/O重定向是指改变数据的输入或输出来源。

1、输入重定向:<

[root@hao ~]# tr 'a-z' 'A-Z' < /etc/fstab

#
# /ETC/FSTAB
# CREATED BY ANACONDA ON SAT JUL 26 20:12:53 2014
#
...

2、在此处生成文档:<< 通常和EOF或END一起使用

[root@hao ~]# cat << END
> the first line
> second
> end
> END
the first line
second
end
[root@hao ~]# cat >> /tmp/myfile.txt << EOF > the first line > second > EOF [root@hao ~]# cat /tmp/myfile.txt the first line second

3、输出重定向:> 覆盖输出。

  会覆盖目标文件中的内容,容易发生错误。可以使用set -C禁止覆盖已经存在的文件。同理set +C则可以关闭上述功能。默认情况下是可以覆盖,当然在set -C 关闭覆盖输出功能情况下,如果要强制覆盖输出,则可以使用>|来强制覆盖输出。

set -C

4、输出重定向:>>追加输出

[root@hao tmp]# ls /var
account  crash  db     games  lib    lock  mail  opt       run    tmp  yp
cache    cvs    empty  gdm    local  log   nis   preserve  spool  www
[root@hao tmp]# ls /var >/tmp/var.out
[root@hao tmp]# cat /tmp/var.out
account
cache
...

5、重定向错误输出:2>,如果不是错误输出,则2>相当于>

6、追加方式重定向错误输出:2>>

[root@hao ~]# ls /varr > /tmp/var2.out
ls: cannot access /varr: No such file or directory
[root@hao ~]# ls /varr 2> /tmp/var2.out
[root@hao ~]# cat /tmp/var2.out
ls: cannot access /varr: No such file or directory
[root@hao ~]# ls /var 2> /tmp/var2.out
account  crash  db     games  lib    lock  mail  opt       run    tmp  yp
cache    cvs    empty  gdm    local  log   nis   preserve  spool  www

7、若为标准输出,则输出到某一个文件,若为错误输出,则重定向到另一个文件

[root@hao ~]# ls /var > /tmp/var2.out 2>/tmp/err.out 
[root@hao ~]# cat /tmp/var2.out
account
cache
crash
...
[root@hao ~]# cat /tmp/err.out

8、重定向标准输出和错误输出至同一个文件:&>

[root@hao ~]# ls /var# &> /tmp/var3.out
[root@hao ~]# cat /tmp/var3.out
ls: cannot access /var#: No such file or directory
[root@hao ~]# ls /var &> /tmp/var3.out
[root@hao ~]# cat /tmp/var3.out
account
cache
...

三、管道                                                                  

管道:把前一个命令的输出,作为后一个命令的输入,以此类推至多个命令。

[root@hao ~]# echo 'hello world' | tr 'a-z' 'A-Z'
HELLO WORLD
[root@hao ~]# cut -d: -f1 /etc/passwd |sort
abrt
adm
apache
...
[root@hao ~]# cut -d: -f3 /etc/passwd |sort -n
0
1
2
3
4
5
6
7
8
...
[root@hao ~]# cut -d: -f1 /etc/passwd |sort|tr 'a-z' 'A-Z'
ABRT
ADM
APACHE
AVAHI-AUTOIPD
BIN
...

四、tee命令,输出到文件中,且输出到屏幕上                

[root@hao ~]# echo 'hello world' | tee /tmp/hello.out
hello world
[root@hao ~]# cat /tmp/hello.out
hello world

五、练习                                                                  

1、统计/usr/bin/目录下的文件个数

[root@hao ~]# ls /usr/bin | wc -l
1479

2、取出当前系统上所有用户的shell,要求每种shell只显示以此,并且按顺序显示

[root@hao ~]# cut -d: -f7 /etc/passwd|sort -u
/bin/bash
/bin/sync
/bin/tcsh
/sbin/halt
/sbin/nologin
/sbin/shutdown

3、显示/var/log目录下每个文件的内容类型

[root@hao ~]# file /var/log/*
/var/log/anaconda.ifcfg.log:   ASCII text
/var/log/anaconda.log:         UTF-8 Unicode English text
/var/log/anaconda.program.log: ASCII English text, with very long lines, with overstriking
/var/log/anaconda.storage.log: UTF-8 Unicode C++ program text, with very long lines
...

4、取出/etc/inittab文件的第六行

[root@hao log]# head -6 /etc/inittab |tail -1
#

5、取出/etc/passwd文件中倒数第9个用户的用户名和shell,显示到屏幕上并将其保存至/tmp/users文件中

[root@hao log]# tail -9 /etc/passwd |head -1|cut -d: -f1,7|tee /tmp/users
tcpdump:/sbin/nologin

6、显示/etc目录下所有一pa开头的文件,并统计其个数

[root@hao log]# ls -d /etc/pa*|wc -l
5

7、不使用文本编辑器,将alias cls=clear 一行内容添加至当前用户的.bashrc文件中。

[root@hao log]# echo "alias cls=clear" >> ~/.bashrc
原文地址:https://www.cnblogs.com/mingcaoyouxin/p/4077264.html