程序:指令+数据
读入数据:input
输出数据:output
打开的文件都有一个fd:file descriptor 文件描述符
Linux给程序提供三种I/O设备
标准输入(STDIN) -0 默认接受来自终端窗口的输入
标准输出(STDOUT)-1 默认输出到终端窗口
标准错误(STDERR)-2 默认输出到终端窗口
[root@centos7 proc]# echo $$
2199
[root@centos7 proc]# ll /proc/$$/fd
总用量 0
lrwx------. 1 root root 64 10月 19 20:09 0 -> /dev/pts/0
lrwx------. 1 root root 64 10月 19 20:09 1 -> /dev/pts/0
lrwx------. 1 root root 64 10月 19 20:09 2 -> /dev/pts/0
lrwx------. 1 root root 64 10月 19 20:22 255 -> /dev/pts/0
[root@centos7 proc]#
[root@centos7 proc]# ll /proc/self/fd
总用量 0
lrwx------. 1 root root 64 10月 19 20:25 0 -> /dev/pts/0
lrwx------. 1 root root 64 10月 19 20:25 1 -> /dev/pts/0
lrwx------. 1 root root 64 10月 19 20:25 2 -> /dev/pts/0
lr-x------. 1 root root 64 10月 19 20:25 3 -> /proc/2222/fd
I/O重定向redirect
格式:命令 操作符号 文件名
支持的操作符号
1> or >:重定向标准输出
2>:重定向标准错误
[root@centos7 ~]# xxx 2> /data/f1.log
[root@centos7 ~]# cat /data/f1.log
-bash: xxx: 未找到命令
[root@centos7 ~]# rm /data/f1.log 2> /data/all.log
y
[root@centos7 ~]# cat /data/all.log
rm:是否删除普通文件 "/data/f1.log"?[root@centos7 ~]#
&>:同时重定向标准输出和错误
[root@centos7 ~]# ls /data/ /rr &> /data/all.log
[root@centos7 ~]# cat /data/all.log
ls: 无法访问/rr: 没有那个文件或目录
/data/:
all.log
以上如果文件已经存在,文件内容会被覆盖
set -c 禁止将内容覆盖已有文件,但可追加;强制覆盖 &|
set +c允许覆盖
追加
’>> 1‘
’2>>‘
’&>>‘
标准输入重定向
<
[root@centos7 ~]# cat bc.log
2+3
[root@centos7 ~]# bc < bc.log
5
[root@centos7 ~]# seq -s+ 1 10 > bc.log
[root@centos7 ~]# bc < bc.log
55
管道符
管道(使用符号“|”表示)用来连接多个命令
将命令1的STDOUT发送给命令2的STDIN,命令2的STDOUT发送到命令3的STDIN
所有命令会在当前shell进程的子shell进程中执行
组合多种工具的功能
注意:STDERR默认不能通过管道转发,可利用2>&1 或 |& 实现,格式如下
[root@centos7 ~]# seq -s+1 100 | bc
15130
管道符前命令有标准输出;管道符后命令有标准输入功能
单行重定向
多行重定向
tr
ls | tr ‘a-z’ ‘A-Z’ #转换为大写字母
ls -l /etc | less #less实现分页地查看输入
echo "test email" | mail #mail通过电子邮件发送输入
echo "2^3" | bc #算术运算
tee
利用tee可以重定向多个目标
功能:
保存不同阶段的输出
复杂管道的故障排除
同时查看和记录输出
练习
1)将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中
[root@centos7 ~]# cat /etc/issue | tr 'a-z' 'A-Z' > /tmp/issue.out
[root@centos7 ~]# cat /tmp/issue.out
\S
KERNEL \R ON AN \M
2)将当前系统登录用户的信息转换为大写后保存至/tmp/who.out文件中
[root@centos7 ~]# who | tr 'a-z' 'A-Z' > /tmp/who.out
[root@centos7 ~]# cat /tmp/who.out
ROOT TTY1 2020-10-18 22:40
ROOT PTS/0 2020-11-06 22:57 (192.168.31.126)
ROOT PTS/1 2020-11-09 14:16 (192.168.1.101)
ROOT PTS/2 2020-11-09 15:50 (192.168.1.101)
3、一个linux用户给root发邮件,要求邮件标题为”help”,邮件正文如下: Hello, I am 用户名,The system version is here,please help me to check it ,thanks! 操作系统版本信息
echo -e "hello ,I am `whoami`,the system version is here ,please help me to check it ,thanks! \n`cat /etc/centos-release`" > /tmp/help
4、将/root/下文件列表,显示成一行,并文件名之间用空格隔开
[root@centos7 ~]# ls -am | tr ',' ' '
5、计算1+2+3+...+99+100的总和
[root@centos7 ~]# echo {1..100} | tr ' ' '+' | bc
5050
6、删除Windows文本文件中的回车字符 ,即“\r”
cat file | tr -d "\r" > file
7、处理字符串“xt.,l 1 jr#!$mn 2 c*/fe 3 uz4”,只保留其中的数字和空格
[root@centos7 ~]# echo “xt.,l 1 jr#filemn 2 c*/fe 3 uz 4“ | tr -d "[:alpha:][:punct:]"
“ 1 2 3 4“
8、将PATH变量每个目录显示在独立的一行
[root@centos7 ~]# echo $PATH | tr ':' '\n'
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/root/bin
9、将指定文件中0-9分别替代成a-j
[root@centos7 ~]# echo 123456789 > a.txt
[root@centos7 ~]# cat a.txt | tr '1-9' 'a-j'
abcdefghi
10、将文件/etc/centos-release中每个单词(由字母组成)显示在独立一行,并无空行
[root@centos7 ~]# cat /etc/centos-release |tr -c "[:alpha:]" " "|tr -s " " "\n"
CentOS
Linux
release
Core