I/O重定向 I/O Redirection

来源:互联网 发布:詹姆斯卡梅隆,知乎 编辑:程序博客网 时间:2024/05/16 20:29

I/O重定向

I/O Redire

- - - - - - - - - - - - - - - - - - - -  - - - - -  -- - - - - -- - - -- - -- - -- - -- - - -- - - - -- - - -- - -- - - - -

  • 标准输入、标准输出、标准错误
  • 输出重定向及综合案例
  • 输入重定向及结合案例

标准输入、标准输出、标准错误


 file descriptors (FD,文件描述符 或 Process I/O channels):
进程使用文件描述符来管理打开的文件
[root@localhost ~]# ls /proc/$$/fd
0 1 2 3 4

0, 1, and 2, known as standard input, standard output, and standard error

输出重定向 (覆盖,追加)
正确输出: 1> 1>> 等价于 > >>
错误输出: 2> 2>>

案例1:输出重定向(覆盖)
[root@localhost ~]# date 1> date.txt 

案例2:输出重定向(追加)
[root@localhost ~]# date >> date.txt 


案例3:错误输出重定向
[root@localhost ~]# ls /home/ /aaaaaa>list.txt
ls: 无法访问/aaaaaa: 没有那个文件或目录
[root@localhost ~]# ls /home/ /aaaaaa>list.txt 2>error.txt //重定向到不同的位置
案例4: 正确和错误都输入到相同位置
[root@localhost ~]# ls /home/ /aaaaaa&>list.txt //混合输出

案例5: 正确和错误都输入到相同位置
[root@localhost ~]# ls /home/ /aaaaaa>list.txt 2>&1 //重定向到相同的位置

案例6:重定向到空设备/dev/null
[root@localhost ~]# ls /home/ /aaaaaa >list.txt 2>/dev/null //空设备,即将产生的输出丢掉
[root@localhost ~]# ls /home/ /aaaaaa&>/dev/null //空设备,即将产生的输出丢掉

cp /etc/passwd /dev/null ???
cp /etc/passwd /etc/passwd1 2>/dev/null ???



案例7:脚本中使用重定向

# vim ping.sh
#!/bin/bash
ping -c1 192.168.2.2 &>/dev/null
if [ $? -eq 0 ];then
echo "up.."
else
echo "down.."
fi
# sh ping.sh

案例8:脚本中使用重定向
# vim ping2.sh
#!/bin/bash
ping -c1 192.168.2.254 &>/dev/null
if [ $? -eq 0 ];then
echo "192.168.2.254 up.." > /up.txt
else
echo "192.168.2.254 down.." >/down.txt
fi
# sh ping2.sh

输入重定向

标准输入: < 等价 0<
案例1:
[root@localhost ~]# mail -s "qwqw" tom  //没有改变输入的方向,默认键盘
aqwdqw
12ed
asdfcav 
EOT
[root@localhost ~]# su - tom
[tom@localhost ~]$ mail
mail           mailq          mailq.postfix  mailstat       mailx
[tom@localhost ~]$ mail
Heirloom Mail version 12.4 7/29/08.  Type ? for help.
"/var/spool/mail/tom": 2 messages 2 new
>N  1 root                  Sat Feb 11 16:20  20/583   "qwqw"

[root@localhost ~]# mail -s "test01" tom< /etc/hosts //输入重定向,来自于文件

案例2:
[root@localhost ~]# grep 'root' //没有改变输入的方向,默认键盘,此时等待输入...
zhang hao
sqwqwdqwd..
asdafavsc..

[root@localhost ~]# grep 'root' < /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

案例3:
[root@localhost ~]# dd if=/dev/zero of=/file1.txt bs=1M count=2
[root@localhost ~]# dd </dev/zero >/file2.txt bs=1M count=20

案例4:mysql表结构导入
[root@localhost ~]# mysql -uroot -p123 < bbs.sql

案例5:at
[root@localhost ~]# at now +5 min
at> useradd yang99
at> <EOT>
job 1 at 2015-06-09 11:57

[root@localhost ~]# vim at.txt
useradd yang100
useradd yang102
[root@localhost ~]# at now +2 min < at.txt
job 2 at 2015-06-09 11:55


综合案例1: 利用重定向建立多行的文件
[root@localhost ~]# echo "111" > file1.txt
[root@localhost  ~]# cat file1.txt
111

[root@localhost ~]# cat >file2.txt
111
222
333
444
^D
[root@localhost  ~]# cat file2.txt 

[root@localhost ~]# cat >>file3.txt
aaa
bbb
ccc
ddd
^D
[root@localhost ~]# cat file3.txt


[root@localhost ~]# cat >file4 <<EOF
> 111
> 222
> 333
> EOF
[root@localhost ~]# cat file4
111
222
333


0 0
原创粉丝点击