Linux学习笔记-IO重定向
1 标准输入和输出*
打开的文件都有一个fd(file description)文件描述符 /proc/
Linux给程序提供三种I/O设备
- 标准输入(STDIN) -0 默认接收来自键盘的输入
- 标准输出(STDOUT) -1 默认输出到终端窗口
- 标准错误(STDERR) -2 默认输出到终端窗口
2 I/O重定向redirect
2.1 标准输出重定向
I/O重定向:改变默认位置
> 输出重定向位置,文件不存在则创建,文件已经存在则覆盖
范例(四个命令中哪个命令的输出结果和其他的不同)
[19:13:35 root@centos8-01 ~]#ls /data /err > /data/all.log 2>&1
[19:13:35 root@centos8-01 ~]#ls /data /err &> /data/all.log
[19:13:35 root@centos8-01 ~]#ls /data /err 2>&1 > /data/all.log #标准错误会重定向给标准输出打印到屏幕上
[19:13:35 root@centos8-01 ~]#ls /data /err 2> /data/all.log 1>&2
>> 追加
set -C/+C 禁止/允许覆盖
&>将错误和标准输出放在同一个文件内
多个命令通过()重定向到文件中
echo 12345678 | passwd --stdin root &> /dev/null
合并多个命令结果到一个文件中
[15:47:18 root@centos8-01 ~]#(cal 2019;cal2020) > /data/cal.txt
[18:33:48 root@centos8-01 ~]#{ ls;hostname;} > /data/all.log
利用标准输入删除大文件
cat /dev/null > /data/file.log
2.2 标准输入重定向
从文件中导入STDIN
[19:25:27 root@centos8-01 data]#cat bc.log
2*3
8+2*3
8*3
[19:26:19 root@centos8-01 data]#bc < bc.log
6
14
24
[19:29:29 root@centos8-01 data]#seq -s+ 1 100 > seq.log
[19:30:08 root@centos8-01 data]#bc < seq.log
#使用管道符减少中间文件
[19:34:18 root@centos8-01 data]#seq -s+ 1 100 | bc
多行重定向(减少磁盘IO)
使用<<终止词 从键盘上把多行导入给STDIN
[19:45:20 root@centos8-01 data]#cat > cat.log << EOF
> aaa
> bbb
> ccc
> ddd
> eee
> fff
> EOF
[19:46:38 root@centos8-01 data]#cat cat.log
aaa
bbb
ccc
ddd
eee
fff
2.2.1 tr命令
转换、压缩和删除字符
tr [OPTION] ... SET1 [SET2]
- -d --delete :删除素有属于第一个字符集的字符
- -s --squeezu-repeats:去重,相邻连续的字符
- -t --trancate-set1:将第一个字符集对应的字符转化为第二个字符集对应的字符
- -c -C --complement:取字符集的补集
[root@centos7-01 ~]# ls /data /err 2>&1 | tr 'a-z' 'A-Z'
LS: CANNOT ACCESS /ERR: NO SUCH FILE OR DIRECTORY
/DATA:
FILE1.LOG
FILE2.LOG
FILE3.LOG
FILE4.LOG
FILE5.LOG
FILE6.LOG
FILE7.LOG
FILE8.LOG
FILE9.LOG
[root@centos7-01 ~]# df | tr -s ' '
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 1918620 0 1918620 0% /dev
tmpfs 1930648 0 1930648 0% /dev/shm
tmpfs 1930648 11832 1918816 1% /run
tmpfs 1930648 0 1930648 0% /sys/fs/cgroup
/dev/mapper/centos-root 20961280 1351628 19609652 7% /
/dev/sda1 1038336 153548 884788 15% /boot
tmpfs 386132 0 386132 0% /run/user/0
[root@centos7-01 ~]# echo {1..100}|tr ' ' '+'|bc
5050
[root@centos7-01 ~]# tr -d '\015' < window.txt > linux.txt
2.3 tee命令
重定向到多个目标
-a 追加
- 保存不同阶段的输出
- 复杂管道的故障排除
- 同事查看和记录输出
范例:
[root@centos7-01 ~]# echo "FBI Warning" | tee -a /data/warn.log
FBI Warning
[root@centos7-01 ~]#
[root@centos7-01 ~]# cat /data/warn.log
FBI Warning
FBI Warning
[root@centos7-01 ~]# cat <<EOF |tee .mailrc
> set from=406552915@qq.com
> set smtp=smtp.qq.com
> set smtp-auth-user=406552915@qq.com
> set smtp-auth-password=
> set smtp-auth=login
> set ssl-verify=ignore
> EOF
set from=406552915@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=406552915@qq.com
set smtp-auth-password=
set smtp-auth=login
set ssl-verify=ignore
范例:
[root@centos7-01 ~]# cat /data/pass.txt
centos
[root@centos7-01 ~]# cat /data/pass.txt | passwd --stdin zhang
Changing password for user zhang.
passwd: all authentication tokens updated successfully.
[root@centos7-01 ~]# echo zhang | passwd --stdin zhang
Changing password for user zhang.
passwd: all authentication tokens updated successfully.
[root@centos7-01 ~]# echo zhang | passwd --stdin zhang &> /dev/null
[root@centos7-01 ~]# passwd --stdin zhang < zhang

浙公网安备 33010602011771号