【问题标题】:How to test if STDIN is terminal in batch?如何批量测试STDIN是否为终端?
【发布时间】:2014-05-08 16:53:38
【问题描述】:

在 shell 中我可以这样做:

if test -t 0  ; then
  echo stdin is a tty
  exit 0
fi

如何批量执行此操作?

【问题讨论】:

    标签: batch-file window stdin


    【解决方案1】:

    已编辑 - 感谢所有测试人员。

    @echo off
        timeout 1 2>nul >nul
        if errorlevel 1 (
            echo input redirected
        ) else (
            echo input is console
        )
    

    timeout命令尝试直接访问控制台,如果批处理文件被执行为会失败

    myBatchFile.cmd < input.txt
    echo something | myBatchFile.cmd
    

    在 Windows XP(W2003 资源工具包超时)、7 和 8.1 上测试。

    【讨论】:

    • +1 它在这里工作!在 Windows 8.1 中测试(我喜欢这个...)
    • timeout 0 2&gt;nul &gt;nul 似乎也可以工作并避免 1 秒的延迟。
    • @rivy,仅凭记忆(4 年后),也许我错了,我相信1 的原因是有某个版本的timeout 可执行文件没有' t 允许 0 作为有效值。但目前我仅限于 W10 64b,至少在这个版本中你是对的。谢谢
    猜你喜欢
    • 2011-10-30
    • 1970-01-01
    • 2018-07-16
    • 2020-02-06
    • 1970-01-01
    • 2017-09-30
    • 2016-01-27
    • 2018-09-01
    相关资源
    最近更新 更多