【发布时间】:2020-06-20 06:16:11
【问题描述】:
在例如 Linux 上,可以在 awk 脚本中将 stderr 称为 fd/2。
示例:if(system("test -e " myfile) != 0) myfile = "fd/2"
我需要在 Windows 上也可以使用的东西 - 请问myfile = fd/2 会在那里吗?
我似乎发现我可以使用exist 来检查文件是否存在。
谢谢。
【问题讨论】:
在例如 Linux 上,可以在 awk 脚本中将 stderr 称为 fd/2。
示例:if(system("test -e " myfile) != 0) myfile = "fd/2"
我需要在 Windows 上也可以使用的东西 - 请问myfile = fd/2 会在那里吗?
我似乎发现我可以使用exist 来检查文件是否存在。
谢谢。
【问题讨论】:
D:\TEMP>type error.txt
The system cannot find the file specified.
D:\TEMP>dir nonexistingfile.txt 2>error.txt
Volume in drive D is HDD
Volume Serial Number is D46B-804B
Directory of D:\TEMP
D:\TEMP>type error.txt
File Not Found
D:\TEMP>
编辑:根据@Raymond 链接的文档,我已经看到在 Windows 下 ist 可以重定向错误。
D:\TEMP>del error
D:\TEMP>echo hallo | gawk "{ print \"Serious error detected!\" > \"/dev/stderr\"; print $0 }" 2>error
hallo
D:\TEMP>type error
Serious error detected!
或者,更多的条件:
D:\TEMP>del error
D:\TEMP>gawk "BEGIN{ for (i=1; i<=4; i++) { if (i%2==0) { myfile=\"/dev/stderr\" } else { myfile=\"/dev/stdout\" }; print i >myfile }}" 2>error
1
3
D:\TEMP>type error
2
4
D:\TEMP>
【讨论】:
gawk 中引用fd/2。可以将语句的输出重定向到 stderr,(见链接)