首页 > 解决方案 > Windows 上的 fd/2 等价物是什么?

问题描述

例如,在 Linux 上,可以在 awk 脚本中将 stderr 称为 fd/2。示例:
if(system("test -e " myfile) != 0) myfile = "fd/2"
我需要在 Windows 上也可以使用的东西 - 请问myfile = fd/2那里有什么?我似乎已经发现我可以exist用来检查文件是否存在。谢谢你。

标签: windowsawkstderr

解决方案


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 链接,我已经看到 ist 可以在 Windows 下重定向错误。

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>

推荐阅读