How to test if STDIN is terminal in batch?

不问归期 提交于 2019-12-19 09:14:10

问题


In shell I can do this:

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

How can I do this in batch?


回答1:


EDITED - Thank you to all the testers.

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

The timeout command tries to directly get access to the console, and this will fail if the batch file is executed as

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

Tested on Windows XP (timeout from W2003 Resource Kit), 7 and 8.1.



来源:https://stackoverflow.com/questions/22726198/how-to-test-if-stdin-is-terminal-in-batch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!