【发布时间】:2020-11-25 06:30:23
【问题描述】:
我的当前目录中有 2 个文件:/home/tushar/Desktop
file1.txt
file4.txt
所以当我点击命令时:
1. ls file{1..4}.txt
输出:
ls: cannot access 'file2.txt': No such file or directory
ls: cannot access 'file3.txt': No such file or directory
file1.txt file4.txt
现在我将 stderr 和 stdout 重定向到文件 res.txt 使用:
2. ls file{1..4}.txt 1>res.txt 2>res.txt
res.txt:
file1.txt
file4.txt
ile2.txt': No such file or directory
ls: cannot access 'file3.txt': No such file or directory
如上,我们可以看到文件res.txt中省略了stderr的部分内容。
现在我将命令更改为:
3. ls file{1..4}.txt 1>res.txt 2>&1
res.txt:
ls: cannot access 'file2.txt': No such file or directory
ls: cannot access 'file3.txt': No such file or directory
file1.txt
file4.txt
上述两个命令完全相同,除了在第二个中我重定向了stderr,其中stdout使用文件描述符(&1)重定向。
现在我知道我应该使用 command 以 附加模式 打开 res.txt,如下所示:
4. ls file{1..4}.txt 1>>res.txt 2>&1
OR
5. ls file{1..4}.txt &> res.txt
但我担心的是:
为什么cmd 2和cmd 3的结果不同?
为什么 cmd 3 即使不使用 >> 符号而不使用 cmd 2 也能得到正确的输出?
【问题讨论】:
-
我假设你的意思是在最后一行 cmd 4 和 cmd 3,对吧?
标签: linux bash shell unix git-bash