首页 > 解决方案 > “超时 - t”与“超时”。有什么实际区别吗?

问题描述

我一直在挑战自己用尽可能少的字符重写我的一个批处理项目,并开始怀疑 -t 是否有任何目的。

显然,以下两个脚本在执行时的行为方式完全相同。

timeout -t 5
echo test
pause
timeout 5
echo test
pause

微软文档(https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/timeout_1)和 ss64 (https://ss64.com/nt/timeout.html)都没有提及有关 -t 是否必要的任何内容。

标签: windowsbatch-filecmd

解决方案


timeout命令自己的/?帮助显示[/T]在括号中,表明它是可选的。

C:\>timeout /?

TIMEOUT [/T] timeout [/NOBREAK]

Description:
    This utility accepts a timeout parameter to wait for the specified
    time period (in seconds) or until any key is pressed. It also
    accepts a parameter to ignore the key press.
...

给定无效参数时显示的错误消息也提到/T,无论它是否在命令行上实际指定。

C:\>timeout ***
ERROR: Invalid value for timeout (/T) specified. Valid range is -1 to 99999.

可选的/T开关可能是为了向后兼容,因为timeout与 NT4/2000 资源工具包一起发布的早期版本没有/T开关。例如,这是timeout.exe来自Windows 2000 Server Resource Kit Supplement 1的。

C:\>dir C:\etc\*.exe | find /i "timeout"
12/02/1999  03:54 PM            62,464 TIMEOUT.EXE

C:\>C:\etc\timeout /?

TIMEOUT - This utility is similar to the DOS PAUSE command.  However, it
          accepts a timeout parameter to specify a length of wait (in seconds)
          at which time it will continue without a key press.

          Written by Eric Brown, Business System Division.
          Copyright (C) Microsoft Corporation, 1992-1995.

Usage -   TIMEOUT <###>
          where <###> is a decimal number of seconds between -1 and 100000.
          The value -1 means to wait indefinitely for a key hit.

推荐阅读