【问题标题】:What is the fd/2 equivalent on Windows?Windows 上的 fd/2 等价物是什么?
【发布时间】: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 来检查文件是否存在。 谢谢。

【问题讨论】:

    标签: windows awk stderr


    【解决方案1】:
    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>
    

    microsoft-docs

    编辑:根据@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,(见链接)
    • 在 OP 中,他们希望有条件地重定向到 stderr。
    • According to the gawk documentation: "这些特殊文件名适用于 gawk 已移植到的所有操作系统,而不仅仅是那些兼容 POSIX 的操作系统:... /dev/stderr = 标准错误输出(文件描述符 2)。”
    • 谢谢,它似乎可以在 Windows 上使用 /dev/stderr,所以使用它:)。
    猜你喜欢
    • 2010-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-02
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多