您的位置:首页 > 理论基础

输出重定向 输入重定向 管道简单介绍

2016-07-30 15:52 405 查看

输出重定向 输入重定向 管道简单介绍

标准输入和输出

我们先来了解下输入和输出的概念:
在计算机中我们了解到计算机的组成部分:其中有输入 输出设备。

输出重定向

对于程序来说:

程序 :指令 + 数据 对于数据来说,数据可以由我们通过键盘输入,或者程序直接使用存储设备上的数据,我们称为读入 数据,程序处理数据后需要返回一个结果,这个结果需要输出出来,我们称为输出数据。在linux系统中,打开的文件都有一个fd:file descriptor (文件描述符)例如:我们使用 nano打开一个文件 #通过ps aux 可以看到我们使用的程序 nano root 21498 0.2 0.3 116404 3052 pts/0 S+ 14:10 0:00 nano test#查看fd的标示状态[root@wCentos7 ~]# ll /proc/21498/fdtotal 0lrwx------. 1 root root 64 Jul 30 14:11 0 -> /dev/pts/0lrwx------. 1 root root 64 Jul 30 14:11 1 -> /dev/pts/0lrwx------. 1 root root 64 Jul 30 14:10 2 -> /dev/pts/0[root@wCentos7 ~]#

Linux给程序提供三种I/O设备

标准输入(STDIN) -------> 0 默认接受来自于键盘的输入标准输出(STDOUT)-----> 1 默认输出到终端窗口标准错误(STDERR) -----> 2 默认输出到终端窗口 我们是可以通过修改 I/O输出和输入可以 修改默认位置的,意思就是:我们可以修改程序输入的数据可以不来至键盘;程序的正确输出可以不输出到屏幕上;程序的错误输出可以不输出到屏幕上。

把输出(正确的)和错误输出重定向到文件

语法如下: 命令 操作符号 文件名支持的炒作符号:> : 把STDOUT重定向到文件;2> : 把STDERR重定向到文件;&> : 把以上两类输出重定向到文件注意: > :文件内容会被覆盖,不过我们也可以设置不覆盖
Set -C : 禁止将内容覆盖已有文件,但可以追加Set +C : 允许覆盖>| : 强制覆盖>> : 在原来的基础上,追加新内容,不覆盖原来的内容

举例说明:

标准输出和错误输出各自重定向到不同位置:

[root@wCentos7 ~]# ls -l / #默认输出至屏幕上的消息total 36 lrwxrwxrwx. 1 root root 7 Jul 21 11:23 bin -> usr/bindr-xr-xr-x. 4 root root 4096 Jul 21 11:35 bootdrwxr-xr-x. 19 root root 3240 Jul 28 08:36 devdrwxr-xr-x. 129 root root 8192 Jul 30 12:53 etcdrwxr-xr-x. 8 root root 87 Jul 30 10:44 homelrwxrwxrwx. 1 root root 7 Jul 21 11:23 lib -> usr/liblrwxrwxrwx. 1 root root 9 Jul 21 11:23 lib64 -> usr/lib64drwxr-xr-x. 2 root root 6 Aug 12 2015 mediadrwxr-xr-x. 2 root root 6 Aug 12 2015 mntdrwxr-xr-x. 3 root root 15 Jul 21 11:31 optdr-xr-xr-x. 447 root root 0 Jul 28 08:36 procdr-xr-x---. 14 root root 4096 Jul 30 10:25 rootdrwxr-xr-x. 38 root root 1120 Jul 29 20:14 runlrwxrwxrwx. 1 root root 8 Jul 21 11:23 sbin -> usr/sbindrwxr-xr-x. 2 root root 6 Aug 12 2015 srvdr-xr-xr-x. 13 root root 0 Jul 28 08:36 sysdrwxr-xr-x. 18 root root 4096 Jul 30 11:21 testdirdrwxrwxrwt. 23 root root 4096 Jul 30 14:28 tmpdrwxr-xr-x. 13 root root 4096 Jul 21 11:23 usrdrwxr-xr-x. 20 root root 4096 Jul 28 12:28 var[root@wCentos7 ~]# ls -l / >/tmp/dui.log 2>/tmp/cuowu.log正确的重定向到 dui.log文件错误的重定向到 cuowu.log文件屏幕上不再显示任何的信息了[root@wCentos7 ~]# cat /tmp/dui.log #查看文件的内容和正确输出信息是一样的total 36lrwxrwxrwx. 1 root root 7 Jul 21 11:23 bin -> usr/bindr-xr-xr-x. 4 root root 4096 Jul 21 11:35 bootdrwxr-xr-x. 19 root root 3240 Jul 28 08:36 devdrwxr-xr-x. 129 root root 8192 Jul 30 12:53 etcdrwxr-xr-x. 8 root root 87 Jul 30 10:44 homelrwxrwxrwx. 1 root root 7 Jul 21 11:23 lib -> usr/liblrwxrwxrwx. 1 root root 9 Jul 21 11:23 lib64 -> usr/lib64drwxr-xr-x. 2 root root 6 Aug 12 2015 mediadrwxr-xr-x. 2 root root 6 Aug 12 2015 mntdrwxr-xr-x. 3 root root 15 Jul 21 11:31 optdr-xr-xr-x. 448 root root 0 Jul 28 08:36 procdr-xr-x---. 14 root root 4096 Jul 30 10:25 rootdrwxr-xr-x. 38 root root 1120 Jul 29 20:14 runlrwxrwxrwx. 1 root root 8 Jul 21 11:23 sbin -> usr/sbindrwxr-xr-x. 2 root root 6 Aug 12 2015 srvdr-xr-xr-x. 13 root root 0 Jul 28 08:36 sysdrwxr-xr-x. 18 root root 4096 Jul 30 11:21 testdirdrwxrwxrwt. 23 root root 4096 Jul 30 14:31 tmpdrwxr-xr-x. 13 root root 4096 Jul 21 11:23 usrdrwxr-xr-x. 20 root root 4096 Jul 28 12:28 var[root@wCentos7 ~]# lss -l / #错误默认输出到屏幕上面来的bash: lss: command not found...Similar command is: 'ls'[root@wCentos7 ~]# lss -l / >/tmp/dui.log 2>/tmp/cuowu.log[root@wCentos7 ~]# cat /tmp/cuowu.log #重定向后,信息保存在文件中了bash: lss: command not found...Similar command is: 'ls'

对个命令的输出重定向语法:

[root@wCentos7 ~]# (cat /etc/issue; cal) > /tmp/all.txt (记得多个命令使用括号 分号隔开)把issue的内容 和 日历的信息重定向到文件中[root@wCentos7 ~]# cat /tmp/all.txt \SKernel \r on an \m ========================welcome to my hostname SAwelcome to my hostname SB========================= July 2016 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 910 11 12 13 14 15 1617 18 19 20 21 22 2324 25 26 27 28 29 3031

对有些命令不管输出的是正确的还是错误的输出我们都丢弃掉不保存任何文件也不需要输出默认输出方法:

[root@wCentos7 ~]# findsss /etc/ -name passwdbash: findsss: command not found...#默认错误输出输出至屏幕上[root@wCentos7 ~]# find /etc/ -name passwd/etc/passwd/etc/pam.d/passwd#默认正确输出输出至屏幕上[root@wCentos7 ~]# find /etc/ -name passwd &>/dev/null[root@wCentos7 ~]#findsss /etc/ -name passwd &>/dev/null#输出重定向后,不管是错误的还是正确的数据在屏幕上都不输出了注意:/dev/null是一个特殊的设备,就是一个数据黑洞,吞噬送往该设备的所有数据,以后如果不想让屏幕有显示可以,会经常用到这个/dev/unll 的数据吞噬设备

输入重定向

关于输入重定向,默认情况下,有许多的指令时等待默认输入设备输入的,比如默认输入设备键盘。等待键盘输入指令有: cat tr等等命令,我们就以实际的案例来了解输入重定向如何使用 。正常情况下你使用cat命令不带任何参数,cat就会另起一行,你输入什么,他就输出什么,和用户就有一个交换的过程,我们如果把需要输入的数据保存在文件中,一起输入到cat中去行不行,是可以的,我们可以这样操作:输入重定向的表示符为: <

Cat命令输入重定向:

[root@wCentos7 ~]# cat < /etc/fstab #我们使用 < 就把fatab文件中的内容输入至cat命名,cat命令在显示在标准输出上。## /etc/fstab# Created by anaconda on Thu Jul 21 11:22:48 2016## 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=b63a2f0c-c9f9-465d-b2b5-3830fedccdd2 / xfs defaults 0 0UUID=9dec22f7-215e-4a47-97d1-ae09446d62d2 /boot xfs defaults 0 0UUID=a138a967-21db-41e0-9a4a-7a5eb4979481 /testdir xfs defaults 0 0UUID=bb99272d-4848-49bd-a896-e5f843037cbf swap swap defaults 0 0

tr 命令输入重定向

[root@wCentos7 yuedir]# tr "a-z" "A-Z" < /etc/issue #把输入进入的数据小写字母转化为大写字母\SKERNEL \R ON AN \M========================WELCOME TO MY HOSTNAME SAWELCOME TO MY HOSTNAME SB=========================

管道

管道(使用符号“|”表示)用来连接命令
命令1 | 命令2 | 命令3 | …将命令1的STDOUT发送给命令2的STDIN,命令2的STDOUT发送到命令3的STDINSTDERR默认不能通过管道转发,可利用2>&1 或|& 实现最后一个命令会在当前shell进程的子shell进程中执行用来组合多种工具的功能举例说明:
[root@wCentos7 yuedir]# cat /etc/issue | tr "a-z" "A-Z" #小写转换为大写\SKERNEL \R ON AN \M========================WELCOME TO MY HOSTNAME SAWELCOME TO MY HOSTNAME SB=========================[root@wCentos7 yuedir]# echo 1234567890 | passwd --stdin harry #自动修改密码Changing password for user harry.passwd: all authentication tokens updated successfully.[root@wCentos7 yuedir]#

总结:

输入重定向与管道感觉功能上有些冲突,管道的功能更好跟强大,支持的命令比输入重定向多。

关于管道的更做的功能在实际运用中慢慢体会,比如: tr 命令 ,tee命令等等,后面介绍命令的使用可以更好的理解管道的使用。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux 计算机 定向