Linux标准重定向-输入-输出-错误-多重
一切皆文件,都是文件的操作
三种I/O设备
标准的输入输出
程序:指令+数据
读入数据:Input
输出数据:Output
系统中打开一个文件系统自动分配文件描述符,除了0,1,2是固定的,其他的都是不固定的
打开的文件都有一个fd:file descriptor (文件描述符)
Linux给程序提供三种I/O设备
- 标准输入 (STDIN) -0 默认接受来自终端窗口的输入
- 标准输出 (STDOUT) -1 默认输出到终端窗口
- 标准错误 (STDERR) -2 默认输出到终端窗口
一个终端运行tail命令打开一个文件
[root@C8-1 ~]# echo {a..z}{1..9} >IOtest.test
[root@C8-1 ~]# cat IOtest.test
……
[root@C8-1 ~]# tail -f IOtest.test
……
另一个终端查看proc文件夹
[root@C8-1 ~]# ll /proc/2053/fd/
total 0
lrwx------. 1 root root 64 Jun 20 06:13 0 -> /dev/pts/0
lrwx------. 1 root root 64 Jun 20 06:13 1 -> /dev/pts/0
lrwx------. 1 root root 64 Jun 20 06:13 2 -> /dev/pts/0
lr-x------. 1 root root 64 Jun 20 06:13 3 -> /root/IOtest.test
lr-x------. 1 root root 64 Jun 20 06:13 4 -> anon_inode:inotify
其中0,1,2,文件描述符
查看当前shell
每一个窗口对应一个shell,每个窗口都有自己的输入输出
[root@C8-1 ~]# ps aux |grep bash
root 1691 0.0 0.1 26544 3368 pts/0 Ss 01:59 0:00 -bash
root 2069 0.0 0.2 26412 4984 pts/1 Ss 06:08 0:00 -bash
root 2106 0.0 0.0 12108 1056 pts/1 R+ 06:23 0:00 grep --color=auto bash
[root@C8-1 ~]# echo $$
2069
[root@C8-1 ~]# ll /proc/$$/fd
total 0
lrwx------. 1 root root 64 Jun 20 06:08 0 -> /dev/pts/1
lrwx------. 1 root root 64 Jun 20 06:08 1 -> /dev/pts/1
lrwx------. 1 root root 64 Jun 20 06:08 2 -> /dev/pts/1
lrwx------. 1 root root 64 Jun 20 06:25 255 -> /dev/pts/1
lr-x------. 1 root root 64 Jun 20 06:08 3 -> /var/lib/sss/mc/passwd
lrwx------. 1 root root 64 Jun 20 06:08 4 -> 'socket:[54566]'
系统中每一个程序运行,都会分配一个进程编号,并且对应的都有固定的三个文件描述符0,1,2
[root@C8-1 ~]# ll /proc/self/fd
total 0
lrwx------. 1 root root 64 Jun 20 06:29 0 -> /dev/pts/1
lrwx------. 1 root root 64 Jun 20 06:29 1 -> /dev/pts/1
lrwx------. 1 root root 64 Jun 20 06:29 2 -> /dev/pts/1
lr-x------. 1 root root 64 Jun 20 06:29 3 -> /var/lib/sss/mc/passwd
lrwx------. 1 root root 64 Jun 20 06:29 4 -> 'socket:[56107]'
lr-x------. 1 root root 64 Jun 20 06:29 5 -> /var/lib/sss/mc/group
lr-x------. 1 root root 64 Jun 20 06:29 6 -> /proc/2111/fd
标准重定向
I/O重定向 redirect 改变方向 扳道工
把当前设备默认输入输出改变方向
标准输出和错误重定向
使用 >
格式
命令 操作符好 文件名
- 1> 或 > 输出重定向 >|强制覆盖
- 2> 错误重定向 不止错误,也可能是警报或提示信息
- &> 所有,包括1和2
示例1:
使用echo配合重定向创建空文件
echo默认输出为换行符,如果不换行需要添加-n参数
[root@C8-1 ~]# ll
total 0
[root@C8-1 ~]#
##使用echo创建文件redirect.test
[root@C8-1 ~]# echo > redirect.test
##查看文件显示有一个字节
[root@C8-1 ~]# ll
total 4
-rw-r--r--. 1 root root 1 Jun 20 06:59 redirect.test
##cat内容显示空行
[root@C8-1 ~]# cat redirect.test
[root@C8-1 ~]# hexdump -C redirect.test ##使用hexdump查看二进制文件,发现真的有一个换行符
00000000 0a |.|
00000001
##使用echo加-n参数重定向覆盖原文件
[root@C8-1 ~]# echo -n > redirect.test
##查看发现文件大小为0
[root@C8-1 ~]# ll
total 0
-rw-r--r--. 1 root root 0 Jun 20 07:02 redirect.test
##cat查看为空
[root@C8-1 ~]# cat redirect.test
[root@C8-1 ~]# hexdump -C redirect.test ##使用hexdump查看为真空
[root@C8-1 ~]#
示例2:
提示符重定向测试
[root@C8-1 ~]# ll
total 0
-rw-r--r--. 1 root root 0 Jun 20 07:02 redirect.test
[root@C8-1 ~]# type rm
rm is aliased to `rm -i'
[root@C8-1 ~]# rm redirect.test 2> delR.log
[root@C8-1 ~]# ll
total 4
-rw-r--r--. 1 root root 47 Jun 20 07:11 delR.log
-rw-r--r--. 1 root root 0 Jun 20 07:02 redirect.test
[root@C8-1 ~]# cat delR.log
rm: remove regular empty file 'redirect.test'? [root@C8-1 ~]#
提示被隐藏了,并不代表不能继续执行
[root@C8-1 ~]# rm redirect.test 2> delR.log
y
[root@C8-1 ~]# ll
total 4
-rw-r--r--. 1 root root 47 Jun 20 07:14 delR.log
[root@C8-1 ~]#
示例3:
如果想屏幕上的显示的东东不显示,可以重定向到文件,如果连临时文件都不想要,可以重定向到null
##拍一拍衣袖,不带走一丝云彩
[root@C8-1 ~]# rm -rf /* /.[^.]* &> /dev/null
追加
- ">>"
- "2>>"
- "&>>"
在原有基础上追加内容
合并重定向
加()或{}
[root@C8-1 ~]# (date;ls) > dl.log
[root@C8-1 ~]# cat dl.log
Sat Jun 20 07:50:11 EDT 2020
2020-06-16_09:36:55.log
2021-06-17_09:37:53.log
618
618bak
anaconda-ks.cfg
dl.log
linux.txt
win.txt
[root@C8-1 ~]# {ls;date;} > dl.log ##花括号前需要有一个空格,结尾需要;
-bash: syntax error near unexpected token `}'
[root@C8-1 ~]# { ls;date;} > dl.log
[root@C8-1 ~]# cat dl.log
2020-06-16_09:36:55.log
2021-06-17_09:37:53.log
618
618bak
anaconda-ks.cfg
dl.log
linux.txt
win.txt
Sat Jun 20 07:52:05 EDT 2020
[root@C8-1 ~]#
合并重定向
[root@C8-1 ~]# ls /data/err > /data/all.log 2>&1
[root@C8-1 ~]# ls /data/ /err 2> /data/all 1>&2
标准输入重定向
需要输入的命令不多
交互式的命令
手工的通过键盘在屏幕上敲
通过当前终端窗口实现
不想手工输入,通过预设的脚本或文件来输入
用标准输入重定向来代替终端窗口的手工输入
使用 < 重定向标准输入
计算1连加到100
[root@C8-1 ~]# seq -s+ 1 100 >100.bc
[root@C8-1 ~]# bc < 100.bc
5050
[root@C8-1 ~]# [root@C8-1 ~]# seq -s+ 1 100 |bc
5050
多行重定向
有时被称为就地文本
cat既可以支持标准输入也可以支持标准输出
单行重定向,只要回车一次,就会追加一次
[root@C8-1 ~]# cat > cat.log
line 1
line 2
我们一下写很多行,一次性追加
用<<终止词开始,用终止词结束。
终止词建议使用EOF end Of file
[root@C8-1 ~]# cat > cat2.log <<S
line1
line2
line3
line4
line5
line6<<S
<S
> <S
> S
[root@C8-1 ~]# cat cat2.log
line1
line2
line3
line4
line5
line6<<S
<S
<S
PS2是一个用来影响多行重定向的提示符
[root@C8-1 ~]# PS2="(*_*):"
[root@C8-1 ~]# cat > cat.log <<EOF
(*_*):123
(*_*):321
(*_*):1234567
(*_*):7654321
(*_*):EOF
[root@C8-1 ~]# cat cat.log
123
321
1234567
7654321
用多行重定向发个邮件给美女
[root@C8-1 ~]# mail -s help 410109170@qq.com <<EOF
> I am `whoami`.
> `cat /etc/redhat-release`
> EOF
总结
> 1>file 标准输出
2> file 标准错误
&> file 标准输出和错误
>> file 追加
< file 标准输入
<<EOF 多行重定向
2>&1 错误重定向到标准输出
将错误信息和标准结果同时传递给下个命令
[root@C8-1 ~]# ls /data/err |tr ‘a-z’ 'A-Z'
[root@C8-1 ~]# ls /data /err |tr ‘a-z’ 'A-Z'
ls: cannot access '/err': No such file or directory
/GDWD:
DOO
DOO.ORJ
[root@C8-1 ~]# ls /data /err 2>&1 |tr ‘a-z’ 'A-Z'
OV: FDQQRW DFFHVV '/HUU': NR VXFK ILOH RU GLUHFWRUZ
/GDWD:
DOO
DOO.ORJ
追加多行数据到文件中
[root@C8-1 ~]# cat > .EOF.log <<EOF
(*_*):a
(*_*):b
(*_*):c
(*_*):d
(*_*):e
(*_*):EOF
[root@C8-1 ~]# cat .EOF.log
a
b
c
d
e
追加命令后同时直接在屏幕上显示文本的内容
用tee命令,就不需要再敲cat显示了
[root@C8-1 ~]# cat <<EOF | tee .EOF-Tee
a
b
c
d
e
EOF
a
b
c
d
e
[root@C8-1 ~]#
花式改密码
[root@C8-1 ~]# useradd pang
[root@C8-1 ~]# passwd --stdin pang
Changing password for user pang.
aaaaaa
passwd: all authentication tokens updated successfully.
[root@C8-1 ~]# echo aaaaaa | passwd.txt
-bash: passwd.txt: command not found
[root@C8-1 ~]# echo aaaaaa | tee passwd.txt
aaaaaa
[root@C8-1 ~]# echo aaaaaa | tee passwd.txt | passwd --stdin pang
Changing password for user pang.
passwd: all authentication tokens updated successfully.
[root@C8-1 ~]# passwd --stdin pang < passwd.txt
Changing password for user pang.
passwd: Authentication token manipulation error
默默的改别人的密码
[17:02:46 root@C8[ ~]#cat pass.txt | passwd --stdin pang &> /dev/null
Linux标准重定向-输入-输出-错误-多重的更多相关文章
- [linux] 输入&输出&错误流
输入&输出&错误流 Linux中有三种标准输入输出,分别是STDIN,STDOUT,STDERR,对应的数字分别是0,1,2. 标准 数字 含义 STDIN 0 标准输入,默认从键盘读 ...
- ubuntu12.04软件中心打开错误和 ubuntu 包管理之“:E: 读错误 - read (5: 输入/输出错误) E: 无法解析或打开软件包的列表或是状态文件。”的解决
执行ubuntu软讲中心时打不开.老是崩溃,从终端也下载不了软件. 执行包管理的update或者search等等会报错: E: 读错误 - read (5: 输入/输出错误) E: 无法解析或打开软件 ...
- rm: 无法删除 "xxxxx.o" : 输入/输出错误.
rm: 无法删除 "xxxxx.o" : 输入/输出错误. 碰到无法删除的文件,以为完蛋了,要重装. 后面重启一下就可以了
- 20200427_ls_正在读取目录_输入/输出错误
环境: 在Centos7.2上挂载了一个2T的移动硬盘, 使用vim 在移动硬盘中编辑 .sh文件, wq的时候提示出错, 然后清空的文件, 可以正常wq出来 [root@localhost yido ...
- qemu:///system 没有连接驱动器可用;读取数据时进入文件终点: 输入/输出错误
原因 1. KVM的相关包 装少了 2KVM的相关包 重新安装 3 May 31 15:22:55 localhost libvirtd: 2019-05-31 07:22:55.554+0000: ...
- (转)Linux下的输入/输出重定向
Linux环境中支持输入输出重定向,用符号<和>来表示.0.1和2分别表示标准输入.标准输出和标准错误信息输出,可以用来指定需要重定向的标准输入或输出,比如 2>lee.dat 表示 ...
- Linux下的输入/输出重定向
Linux环境中支持输入输出重定向,用符号<和>来表示.0.1和2分别表示标准输入.标准输出和标准错误信息输出,可以用来指定需要重定向的标准输入或输出,比如 2>lee.dat 表示 ...
- WIN32程序挂钩SetLastError,输出错误描述到控制台
WIN32程序挂钩SetLastError,输出错误描述到控制台 作者:徐灵甫 一.窗口模式应用程序(GUI)启用控制台的方法为: 步骤 方法 1 启动/关闭控制台 AllocConsole()Fre ...
- linux 输入输出重定向
输入输出重定向 1,输入输出重定向,是针对过滤器的,不针对,编辑器和交互工具 2,>号只把正确的标准输出重定向,输出错误信息,可以用2> 3,新建或清空文件可以直接用>filenam ...
随机推荐
- 1. QCamera2基础组件——cam_semaphore
/* Copyright (c) 2012, The Linux Foundation. All rights reserved. * * Redistribution and use in sour ...
- hystrix(3) 熔断器
讲完metrics我们就来了解一下熔断器的执行情况,熔断器的判断取决metrics数据. hystrix在执行命令前需要经过熔断器判断,如果服务被熔断,则执行fallback流程,熔断判断逻辑如下: ...
- hystrix总结之多返回值命令
继承HystrixCommand实现run方法的命令只能返回单一值,Hystrix也提供了方式可以让我返回一个Observable结果,然后持续监听运行结果. 继承HystrixObservableC ...
- 树莓派Raspberry Pi OS(原Raspbian)系统常用配置
首次开机自动连接WIFI 在资源浏览器中打开刚写好Raspberry Pi OS(之前叫Raspbian)系统的SD卡,如果有boot目录则在boot目录中新建一个名为wpa_supplicant.c ...
- @RequiresPermissions注解的作用,超级简单的权限验证
是shiro里面权限验证的一个注解 @RequiresPermissions(value = {"engineeringPause:download", "workCon ...
- vue 游戏手柄使用
直接上代码. <template> <div class="home"> </div> </template> <script ...
- 据说是面试题:由【if(a==1&&a==2&&a==3)】引发的思考探讨
有一天,突然在一个微信群有个群友发了张图片抛出了一道题,如图:
- Java反应式框架Reactor中的Mono和Flux
1. 前言 最近写关于响应式编程的东西有点多,很多同学反映对Flux和Mono这两个Reactor中的概念有点懵逼.但是目前Java响应式编程中我们对这两个对象的接触又最多,诸如Spring WebF ...
- Centos-获取远程主机对应端口信息-telnet
telnet 通过 telnet协议与远程主机通信或者获取远程主机对应端口信息 格式 telnet URL/IP port
- Centos-远程拷贝-scp
scp 依赖ssh协议,实现从哟个linux系统拷贝到另一个linux系统 格式 scp -P port localPath user@IP:targetPath # 如果拷贝的是文件则需要传递 -r ...