重定向常用方法总结

1570阅读 1评论2012-08-01 公子月
分类:LINUX

1、<<<是把后面的当成字符串传给前面的命令
[root@localhost tmp]# wc <<< "aaaaa"
1 1 6
[root@localhost tmp]# echo aaaaa |wc
      1       1       6
[root@localhost tmp]# echo -n "aaaaa" |wc
      0       1       5
[root@localhost tmp]#
 
2、<是把后面的当成文件传给前面的命令
[root@localhost tmp]# wc < aaaa
-bash: aaaa: No such file or directory
[root@localhost tmp]# wc
 28  28 488
 
3、<<把后面的当成一个分界符,然后从标准输入读取内容,直到遇到下一个分界符结束。
[root@localhost tmp]# wc << aaaa
> zxm
> aaaa
1 1 4
 
4、>和>>都是把前面的输出重定向到后面的文件,后面的表示追加。
[root@localhost tmp]# ls  >a.tt 
[root@localhost tmp]# ls  >>a.tt
 
5、把b.tt文件传给cat命令,把结果重定向到a.tt,整个过程相当于复制。
[root@localhost tmp]# cat a.tt
[root@localhost tmp]# cat >a.tt
 
6、重定向的举例
[root@localhost soft]# ls a.tt b.tt
ls: cannot access b.tt: No such file or directory
a.tt
[root@localhost soft]# ls a.tt b.tt >log.tt
ls: cannot access b.tt: No such file or directory
[root@localhost soft]# ls a.tt b.tt >log.tt 2>&1
[root@localhost soft]# ls a.tt b.tt &>log.tt
[root@localhost soft]# ls a.tt b.tt 2>&-
a.tt
[root@localhost soft]# ls a.tt b.tt 2>&- 1>&-
[root@localhost soft]# ls a.tt b.tt &>/dev/null
[root@localhost soft]# ls a.tt b.tt 1>&-    
ls: cannot access b.tt: No such file or directory
ls: write error: Bad file descriptor
[root@localhost soft]# 

文章评论

  • 2012-08-01公子月

    <img src="/image/face/11.gif" >