【发布时间】:2014-05-08 16:53:38
【问题描述】:
在 shell 中我可以这样做:
if test -t 0 ; then
echo stdin is a tty
exit 0
fi
如何批量执行此操作?
【问题讨论】:
标签: batch-file window stdin
在 shell 中我可以这样做:
if test -t 0 ; then
echo stdin is a tty
exit 0
fi
如何批量执行此操作?
【问题讨论】:
标签: batch-file window stdin
已编辑 - 感谢所有测试人员。
@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 上测试。
【讨论】:
timeout 0 2>nul >nul 似乎也可以工作并避免 1 秒的延迟。
1 的原因是有某个版本的timeout 可执行文件没有' t 允许 0 作为有效值。但目前我仅限于 W10 64b,至少在这个版本中你是对的。谢谢