如何使用bash重定向时,LS看到未来吗? [英] How can ls see the future when using bash redirection?
本文介绍了如何使用bash重定向时,LS看到未来吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我满足以下code在一本书,仍然不明白为什么。任何人都可以帮助解释它吗?
[根@ wd00070319测试]#LS
[根@ wd00070319测试]#触摸文件1
[根@ wd00070319测试]#LS
文件1
[根@ wd00070319测试]#LS>文件2
[根@ wd00070319测试]#LS
文件1文件2
[根@ wd00070319测试]#file2的猫
文件1
文件2
[根@ wd00070319测试]#
解决方案
[根@ wd00070319测试]#LS#列出文件。 (目录是空的!)
[根@ wd00070319测试]#触摸文件1#创建一个名为文件1的空文件
[根@ wd00070319测试]#LS#再次列出文件(见文件1)
文件1
[根@ wd00070319测试]#LS>文件2#列表中的文件,把结果在文件2
[根@ wd00070319测试]#LS#再次列出文件,
文件1文件2
[根@ wd00070319测试] file2的排名,猫文件2#显示内容
文件1
文件2#< - 请注意,文件2是present为好。
[根@ wd00070319测试]#
这里的教训是可能是像 LS&GT的命令;文件2
创建输出文件(文件2
)的前的实际执行 LS
命令。
此行为是由bash的参考手册证实:
3.6重定向
[...] 的前执行一个命令,它的输入和输出可能会被重定向
使用由壳PTED一个特殊的符号间$ P $。的[...]
块引用>I meet the following code in a book and still not understand why. Anyone can help to explain it please?
[root@wd00070319 test]# ls [root@wd00070319 test]# touch file1 [root@wd00070319 test]# ls file1 [root@wd00070319 test]# ls > file2 [root@wd00070319 test]# ls file1 file2 [root@wd00070319 test]# cat file2 file1 file2 [root@wd00070319 test]#
解决方案[root@wd00070319 test]# ls # list the files. (directory is empty!) [root@wd00070319 test]# touch file1 # create an empty file called "file1" [root@wd00070319 test]# ls # list the files again (to see "file1") file1 [root@wd00070319 test]# ls > file2 # list the files, put the result in "file2" [root@wd00070319 test]# ls # list the files again, file1 file2 [root@wd00070319 test]# cat file2 # show content of file2 file1 file2 # <-- Note that "file2" is present as well. [root@wd00070319 test]#
The lesson here is probably that a command like
ls > file2
creates the output-file (file2
) before actually executing thels
command.
This behavior is confirmed by the bash reference manual:
3.6 Redirections
[...] Before a command is executed, its input and output may be redirected using a special notation interpreted by the shell. [...]
这篇关于如何使用bash重定向时,LS看到未来吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文