每天一点Linux-13I/O重定向
I/O重定向 I/O Redirection
标准输入、标准输出、标准错误
进程在运行是会打开多个文件,每个文件都有一个file descriptors (FD,文件描述符 或 Process I/O channels):
进程使用文件描述符来管理打开的文件
[yang@ecs-ea9d ~]$ ls /proc/$$/fd
0 1 2 255
输出重定向 (覆盖,追加)
正确输出: 1> 1>> 等价于 > >>
错误输出: 2> 2>>
案例1:输出重定向(覆盖)
[yang@ecs-ea9d ~]$ date 1>date.txt
案例2:输出重定向(追加)
[yang@ecs-ea9d ~]$ date >>date.txt
案例3:错误输出重定向
[yang@ecs-ea9d ~]$ ls /home/ /aaaaa>list.txt
ls: cannot access /aaaaa: No such file or directory
[yang@ecs-ea9d ~]$ ls /home/ /aaaaaa>list.txt 2>error.txt //重定向到不同的位置
案例4: 正确和错误都输入到相同位置
[yang@ecs-ea9d ~]$ ls /home/ .aaaa &>list1 //混合输出
案例5: 正确和错误都输入到相同位置
[yang@ecs-ea9d ~]$ ls /home/ /aaaa>list.txt3 2>&1 //重定向到相同的位置
案例6:重定向到空设备/dev/null //垃圾桶
[yang@ecs-ea9d ~]$ ls /home/ /aaaaaaaaa >list.txt 2>/dev/null //空设备,即将产生的输出丢掉
[yang@ecs-ea9d ~]$ ls /home/ /aaaaaaaaa &>/dev/null //空设备,即将产生的输出丢掉
案例7:脚本中使用重定向
vim ping.sh
#!/usr/bin/bash
ping -c1 172.16.120.254 &>/dev/null
if [ $? -eq 0 ];then
echo "up.."
else
echo "down.."
fi
bash ping.sh
案例8:脚本中使用重定向
vim ping2.sh
#!/user/bin/bash
ping -c1 www.baidu.com &>/home/yang/workspace/test1
if [ $? -eq 0 ];then
echo “up..”
else
echo"down.."
fi
bash ping2.sh
输入重定向
标准输入: < 等价 0<
案例1:
[yang@ecs-ea9d ~]$ mail -s “drobp” jack
d
r
o
b
p
^D //ctrl+d用来结束内容书写
[yang@ecs-ea9d ~]$ su - jack
[jack@ecs-ea9d ~]$ mail
Mail version 8.1 6/6/93. Type ? for help.
“/var/spool/mail/alice”: 1 message 1 new
N 1 root@ecs-ea9d.local Mon Oct 29 14:09 18/657 “ssss”
&
[jack@ecs-ea9d ~]$ mail -s “test01” jack < /etc/hosts //输入重定向,来自于文件
案例2:
[yang@ecs-ea9d ~]$ grep ‘root’ //没有改变输入的方向,默认键盘,此时等待输入…
yang sss
sssrootssss..
sssrootssss..
[yang@ecs-ea9d ~]$ grep ‘root’ < /etc/passwd
root:x :0:0:root:/root:/bin/bash
operator:x :11:0:operator:/root:/sbin/nologin
案例3:
[yang@ecs-ea9d ~]$ dd if=/dev/zero of=/file1.txt bs=1M count=2 //加限制大小为1M,否则文件会不断变大
[yang@ecs-ea9d ~]$ dd </dev/zero >/file2.txt bs=1M count=20
案例4:mysql表结构导入
[yang@ecs-ea9d ~]$ mysql -uroot -p123 < bbs.sql
案例5:at
[yang@ecs-ea9d ~]$ at now +5 min
at> useradd yang99
at>^D
job 1 at 2015-06-09 11:57
[yang@ecs-ea9d ~]$ vim at.txt
sudo useradd yang100
sudo useradd yang102
[yang@ecs-ea9d ~]$ at now +2 min < at.txt
job 2 at 2015-06-09 11:55
sudo: sorry, you must have a tty to run sudo
sudo: sorry, you must have a tty to run sudo
解决方法:查看修改/etc/sudoers 文件内容(关闭sudo需要终端功能)
综合案例1: 利用重定向建立多行的文件
[yang@ecs-ea9d ~]$ echo “111” > file1.txt
[yang@ecs-ea9d ~]$ cat file1.txt //cat后如果没有要查看的文件就需要在键盘输入
111
^D
[yang@ecs-ea9d ~]$ cat >file2.txt
111
222
333
444
^D
[yang@ecs-ea9d ~]$ cat >file4 <<EOF //EOF定义一个结束符,但此结束符不写入文件中
111
222
333
EOF
[yang@ecs-ea9d ~]$ cat file4
111
222
333
综合案例2
[yang@ecs-ea9d ~]$ ls; date &>/dev/null
[yang@ecs-ea9d ~]$ ls &>/dev/null; date &>/dev/null
[yang@ecs-ea9d ~]$ (ls; date) &>/dev/null
[yang@ecs-ea9d ~]$ (while :; do date; sleep 2; done) &>date.txt
[yang@ecs-ea9d ~]$ (while :; do date; sleep 2; done) &>date.txt &
[1] 6595
[yang@ecs-ea9d ~]$ tailf date.txt
Tue Apr 12 22:04:32 CST 2016
Tue Apr 12 22:04:34 CST 2016
Tue Apr 12 22:04:36 CST 2016
Tue Apr 12 22:04:38 CST 2016
Tue Apr 12 22:04:40 CST 2016
Tue Apr 12 22:04:42 CST 2016
Tue Apr 12 22:04:44 CST 2016
Tue Apr 12 22:04:46 CST 2016
Tue Apr 12 22:04:48 CST 2016
[yang@ecs-ea9d ~]$ jobs
[1]+ Running ( while :; do
date; sleep 2;
done ) &>date.txt &
[yang@ecs-ea9d ~]$ kill %1
[yang@ecs-ea9d ~]$ jobs
[yang@ecs-ea9d ~]$ (./configure && make && make install) &>/dev/null
扩展点:subshell
[yang@ecs-ea9d ~]$ (umask 777; touch file8888)
[yang@ecs-ea9d ~]$
[yang@ecs-ea9d ~]$ ll file8888
———- 1 root root 0 Apr 12 22:11 file8888
[yang@ecs-ea9d ~]$
[yang@ecs-ea9d ~]$ umask
0022
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 2924854739@qq.com
文章标题:每天一点Linux-13I/O重定向
本文作者:DROBP
发布时间:2019-08-21, 19:44:35
最后更新:2019-08-21, 19:51:03
原始链接:https://DROBP.github.io/2019/08/21/每天一点Linux-13I-O重定向/版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。