加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

在使用bash重定向时,我怎么能看到未来?

发布时间:2020-12-15 21:30:25 所属栏目:安全 来源:网络整理
导读:我在书中遇到以下代码,仍然不明白为什么.有人可以帮忙解释一下吗? [root@wd00070319 test]# ls[root@wd00070319 test]# touch file1[root@wd00070319 test]# lsfile1[root@wd00070319 test]# ls file2[root@wd00070319 test]# lsfile1 file2[root@wd0007031
我在书中遇到以下代码,仍然不明白为什么.有人可以帮忙解释一下吗?

[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]#

这里的教训可能是像ls>这样的命令. file2在实际执行ls命令之前创建输出文件(file2).

bash参考手册确认了此行为:

3.6 Redirections

[…] Before a command is executed,its input and output may be redirected using a special notation interpreted by the shell. […]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读