如何测试是否STDIN是批量终端? [英] How to test if STDIN is terminal in batch?

查看:143
本文介绍了如何测试是否STDIN是批量终端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在外壳我可以做到这一点:

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?

推荐答案

编辑 - 感谢所有测试者

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

测试在Windows XP(从W2003资源包超时),7和8.1。

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

这篇关于如何测试是否STDIN是批量终端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆