加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

batch-file – 如何测试STDIN是批量终端?

发布时间:2020-12-14 03:59:35 所属栏目:Windows 来源:网络整理
导读:在 shell中我可以这样做: if test -t 0 ; then echo stdin is a tty exit 0fi 我怎么能批量做到这一点? 解决方法 编辑 – 感谢所有测试人员. @echo off timeout 1 2nul nul if errorlevel 1 ( echo input redirected ) else ( echo input is console ) tim
在 shell中我可以这样做:

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

我怎么能批量做到这一点?

解决方法

编辑 – 感谢所有测试人员.

@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 Resource Kit超时),7和8.1.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读