您的位置:首页 > 运维架构 > Linux

I/O重定向和管道

2016-07-30 18:52 288 查看
一.I/O重定向

1.标准输入和输出
程序=指令+数据
数据又分为:input(读入数据)+output(输出数据)
linux给程序提供三种I/O设备
标准输入(stdin)-0 默认接受来自键盘的输入
标准输出(stdout)-1 默认输出到终端窗口
标准错误(stderr)-2,默认输出到终端窗口
2.输出重定向
(1)覆盖从定向 :>
command >/path/to/file
实例:a重定向覆盖a1
[root@localhost testdir]# cat a1
[root@localhost testdir]# cat a
hello world!
[root@localhost testdir]# cat a >a1
[root@localhost testdir]# cat a1
hello world!
(2)追加重定向:>>
command >>/path/to/file
实例:把etc/issue内容追加到a1中
[root@localhost testdir]# cat /etc/issue >>a1
[root@localhost testdir]# cat a1
hello world!
CentOS release 6.8 (Final)
Kernel \r on an \m
(3)错误输出:2>或2>>
command 2>/path/to/file 覆盖输出
command 2>>/path/to/file追加输出
(4)标准输出和错误输出
command &>/path/to/file
command &>>/path/to/file
或command >/path/to/file 2>&1
command >>/path/to/file 2>>&1
实例:
[root@localhost testdir]# cat /etc/issue /etc/abc &>f1
[root@localhost testdir]# cat f1
CentOS release 6.8 (Final)
Kernel \r on an \m                        #正确输出信息

cat: /etc/abc: No such file or directory  #错误输出信息
上述实例也可以这样写
[root@localhost testdir]# cat /etc/issue /etc/abc >f2 2>&1
[root@localhost testdir]# cat f2
CentOS release 6.8 (Final)
Kernel \r on an \m

cat: /etc/abc: No such file or directory
(5)用cat直接将输入的信息输出到catfile中,当键盘输入eof时输入结束,利用<<eof,我们可以终止一次输入
实例:
[root@localhost testdir]# cat <catfile <<eof
> hello world
> how are you
> eof
-bash: catfile: No such file or directory
实例:向zhang用户写一封求助信
[root@localhost testdir]# mail -s "help" zhang <<eof
> hello,I am `id -nu` #查看用户
> the system version is here,please check it.
> `cat /etc/issue` #查看内核版本
> eof
(6)tr 转换和删除字符
tr[-ds] SET1
-d:删除信息当中的SET1这个字符串
-s:替换掉重复的字符
实例:
将last输出的信息中所有的小写字符变成大写
[root@centos7 ~]# last | tr [a-z] [A-Z]
ROOT     PTS/1        10.1.250.64      SAT JUL 30 12:54   STILL LOGGED IN
ZHANG    PTS/2        10.1.250.64      SAT JUL 30 12:54   STILL LOGGED IN
ZHANG    PTS/1        :0               SAT JUL 30 12:50 - 12:54  (00:03)
ZHANG    PTS/0        :0               SAT JUL 30 12:50   STILL LOGGED IN
......
将/etc/passwd中的冒号删除
[root@localhost testdir]# cat /etc/passwd | tr -d ':'
rootx00root/root/bin/bash
binx11bin/bin/sbin/nologin
daemonx22daemon/sbin/sbin/nologin
admx34adm/var/adm/sbin/nologin
......
将/root/下文件列表,显示成一行,并文件名之间用空格隔开。
[root@centos7 /]# ls -a /root | tr '\n' ' '
. .. anaconda-ks.cfg .bash_history .bash_logout .bash_profile .bashrc .cache cat .config .cshrc .dbus -h history.log initial-setup-ks.cfg mm .tcshrc
二.管道
整体管道命令如下所示:
command1 | command2 | command3
注意;(1)每个管道后面接的第一个数据必须是"命令",例如:less,more,head,tail等
(2)管道命令必须会处理standard output,对于standard error output予以忽略
(3)管道命令必须能接收来自前一个命令的数据成为standard input继续处理才行
下面来介绍一些命令:
1.cut
cut -d'分隔字符' -f files
cut -c 字符范围
选项:
-d:后面跟分隔字符,与-f一起使用
-f:依据-d的分隔字符将一段信息切割成数段,用-f取出第几段
-c:以字符的单位取出固定字符区间
实例:
将PATH变量取出,显示第3个路径
[root@centos7 /]# echo $PATH | cut -d: -f 3
/usr/sbin
2.重定向到多个目标
tee 屏幕显示并继续进行管道处理
command1 |tee file | commmand2
实例:
当前登录用户显示到屏幕并转换为大写
[root@centos7 /]# who |tee f4 | tr 'a-z' 'A-Z'
ZHANG    :0           2016-07-30 12:50 (:0)
ZHANG    PTS/0        2016-07-30 12:50 (:0)
ZHANG    PTS/2        2016-07-30 12:54 (10.1.250.64)
ROOT     PTS/1        2016-07-30 16:04 (10.1.250.64)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux 基础