Linux:I/O重定向和管道使用

本文介绍Linu系统中I/O重定向与管道的基础概念,并通过多个实例详细演示了如何使用这些功能来改变默认输入输出位置,包括标准输出、标准错误的重定向及合并,以及输入重定向和管道的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I/O重定向和管道使用

概念理解

linu系统中的三种I/O设备所代表的编号分别是:
标准输入(STDIN),文件描述符为0,默认从键盘获取输入;
标准输出(STDOUT),文件描述符为1,默认输出到显示屏;
标准错误(STDERR),文件描述符为2,默认输出到显示屏。

I/O重定向就是为了改变默认输入、输出的位置:

>:表示标准输出覆盖重定向;

>>:表示标准输出追加重定向;

2>:表示错误输出覆盖重定向;

2>>:表示错误输出追加重定向;

&>:表示合并标准输出和错误输出覆盖重定向;

&>>:表示合并标准输出和错误输出追加重定向;

2>&1:表示意义同&>即合并标准输出和错误输出覆盖重定向;

<:输入重定向;

<<:多行输入;

set -C命令:禁止覆盖重定向;

>|:强制覆盖重定向(与set -C相反);

set +C命令:解除禁止覆盖重定向的设置;

​ 管道符(|)作用是把前一个命令的执行结果当做后一个命令的输入。

实战演练

1、重定向

【例1】把/etc/fstab文件内容重定向到/data目录下文件名为fstab.out

[root@centos7 ~]#cat /etc/fstab > /data/fstab.out

【例2】把hello world追加到/tmp/fstab.out文件尾部

[root@centos7 ~]# echo "hello world" >>/tmp/fstab.out 

【例3】禁止覆盖重定向和强制重定向

[root@centos7 ~]# set -C
[root@centos7 ~]# echo "hello world" >/dadda/fstab.out
-bash: /tmp/fstab.out: cannot overwrite existing file

设置禁止覆盖重定向后,可强制覆盖重定向

[root@centos7 ]#echo "hello world" >| /data/fstab.out 
[root@centos7 ]#cat fstab.out 
hello world

【例4】解除禁止覆盖重定向设置

[root@centos7 ~]# set +C

【例5】把标准错误覆盖重定向到haha.out文件

先看下只重定向标准输出的情况:

[root@centos7 data]#haha cat > /data/haha.out
bash: haha: command not found...
[root@centos7 data]#cat /data/haha.out 

再看把标准错误重定向的情况:

[root@centos7 data]#haha cat 2> /data/haha.out
[root@centos7 data]#cat /data/haha.out 
bash: haha: command not found...

【例6】把标准错误和标准输出分别重覆盖定向到不同的文件里,即标准错误重定向到wrong.txt文件,标准输出重定向到correct.txt

[root@centos7 ~]#which cat 2> wrong.txt >correct.txt
[root@centos7 ~]#cat wrong.txt 
[root@centos7 ~]#cat correct.txt 
/usr/bin/cat
- - - - - - - - - - - - - - - - - - - - - - - - - 
[root@centos7 ~]#haha cat 2> wrong.txt > correct.txt
[root@centos7 ~]#cat wrong.txt 
bash: haha: command not found...
[root@centos7 ~]#cat correct.txt

【例7】合并标准输出和标准错误覆盖重定向到out.txt文件里

[root@centos7 ~]#which cat &>out.txt
[root@centos7 ~]#cat out.txt        
/usr/bin/cat
[root@centos7 ~]#whichh cat &>>out.txt
[root@centos7 ~]#cat out.txt          
/usr/bin/cat
bash: whichh: command not found...
Similar command is: 'which'  

【例8】&>等价于2&>1,功能同上

[root@centos7 ~]#which cat > out.txt 2>&1
[root@centos7 ~]#cat out.txt 
/usr/bin/cat
[root@centos7 ~]#whichh cat >> out.txt 2>&1
[root@centos7 ~]#cat out.txt               
/usr/bin/cat
bash: whichh: command not found...
Similar command is: 'which'

2、输入重定向和tr命令

tr命令是把字符集1转换为字符集2

【例9】用输入重定向的方式,把所有小写字母转换为大写

[root@centos7 ~]#cat /etc/issue
\S
Kernel \r on an \m
[root@centos7 ~]#tr a-z A-Z </etc/issue
\S
KERNEL \R ON AN \M

从文件导入标准输入

【例10】把f1.txt文件里的内容,写到f2.txt文件里

[root@centos7 data]#cat f1.txt
dushan
[root@centos7 data]#cat >f2.txt<f1.txt
[root@centos7 data]#cat f2.txt 
dushan

多行输入:<<终止词

【例11】屏幕随便输入几行内容,遇到EOF字样结尾后,屏幕内容自动保存在f1.txt里

[root@centos7 data]#cat >f1.txt<<EOF
> dushan
> bangbang
> EOF
[root@centos7 data]#cat f1.txt 
dushan
bangbang

【例12】使用mail命令root给shine普通用户发邮件,要求邮件标题为”help”,邮件正文如下:

Hello, I am 用户名,The system version is here,please help me to check it thanks!

操作系统版本信息

[root@centos7 ~]# mail -s "help" shine <<EOF

> Hello, I am `who`,The system version is here,please help me to check it thanks!

> `cat /etc/redhat-release`

> EOF

4、管道符:|

【例13】把echo输出的内容,传递给tr命令,实现小写字母转换为大写字母

[root@centos7 ~]#echo 'dushan' |tr a-z A-Z
DUSHAN

【例14】一页一页的查看输入

[root@centos7 ~]#ls -l /etc|less
total 1468
drwxr-xr-x.  3 root root      101 Jul 18 17:16 abrt
-rw-r--r--.  1 root root       16 Jul 18 17:21 adjtime
-rw-r--r--.  1 root root     1518 Jun  7  2013 aliases
drwxr-xr-x.  2 root root       51 Jul 18 17:16 alsa
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值