为批处理文件中的单个命令打开回显

Turn on echo for a single command in a batch file

在 Windows 批处理文件中,如果我启用了回显(默认设置),我可以通过在 @ 前加上前缀来禁用回显单个命令,例如:

some_command_that_will_be_echoed
@some_command_that_wont_be_echoed

然而,我用 @echo off 禁用了回显,但有时我 喜欢回显命令。有一些神奇的等价物吗?例如:

@echo off
some_command_that_wont_be_echoed
<unknown_magic_prefix> some_command_that_will_be_echoed

我发现了这个问题:turn on echo temporarily,但实际上是 'turn echo on, do your thing, then turn it off again'。我可以那样做,但我希望能有更优雅的东西。

编辑:这个 出现在相关问题列表中;基本上和我的问题一样,差不多成功了!

EDIT2:为了提供上下文,下面是我所做的一部分。请注意,为了 post 的好处,我用 C 风格的注释装饰了它;他们不在真正的剧本中。

@echo off   // Because I don't want the bulk of the commands displayed. 
            // (In fact it's worse than that, this file is called from another
            //  with echo already disabled.)

title CMD Window with SVN enabled

set PATH=  ...  // my new path which happens to include SVN this time

svn --version --quiet   // This command outputs just the SVN version as a number
                        // but it gets rather lost on its own, and the 'full'
                        // output is too wordy.  My belief is that if the entire
                        // command was echoed, it would provide a perfect 
                        // context.

echo SVN version is:    // This is an obvious alternative, simply putting a
svn --version --quiet   // message before the actual command.

echo svn --version --quiet  // Or even just echoing the command 'by hand'
svn --version --quiet

+@svn --version --quiet   // This imaginary syntax would be the gold-standard

always_echo( svn --version --quiet )   // This function would be acceptable, but
                                       // its definition eludes me and seems 
                                       // clunky

<some batch file magic preamble>    // This would be okay, but would get arduous
svn --version --quiet               // if it was long-winded or had to be done a
<some batch file magic to close>    // few times or more.

但要强调的是,虽然这是一个相对容易 'fix' 的特定示例,但我正在寻找一个更通用的解决方案,我可以在其他可能不那么简单的情况下使用它。

@Compo 有理有据地指出,@echo off 本身就不是很优雅。这很公平,但是 a) 在这种情况下它已经启用并且 b) 它非常惯用。我喜欢这个(虚构语法)的清晰度:

@echo off
command_1    // not echoed
command_2    // not echoed
+@command_3  // IS echoed and stands out
command_4    // not echoed

对比

@command_1    // not echoed
@command_2    // not echoed
command_3     // IS echoed and gets a little lost
@command_4    // not echoed

显然这是一个主观意见,但归结为只装饰不常见情况的愿望。

最后,如果 真的 不可能,那么很公平,但是确定 没有坏处:)

下面是一个可能对您有所帮助的示例:

@(
    Echo On
    Title CMD Window with SVN enabled
    Set "PATH=%PATH%;D:\ummy\directory"
)
svn --version --quiet
@(
    Rem All other none echoed commands here
    Pause
    Echo Off
    Exit /B
)

你可以使用一个小宏(%+@%),它会显示命令然后执行。

@echo off    
call :define_macro

setlocal EnableDelayedExpansion
set var=content

%+@% ver
%+@% ver /?
%+@% echo %var% !var!
exit /b

:define_macro
(set \n=^^^

)
set ^"+@=for %%# in (1 2) do if %%#==2 (%\n%
    setlocal EnableDelayedExpansion %\n%
    for /F "tokens=1,*" %%1 in ("!time: =0! !argv!") do (%\n%
        endlocal%\n%
        echo %%1 Execute: %%2%\n%
        endlocal%\n%
        %%2%\n%
      )%\n%
    ) ELSE setlocal DisableDelayedExpansion ^& set argv="

exit /b

输出:

14:04:08,05 Execute: ver

Microsoft Windows [Version 10.0.17134.1069]
14:04:08,05 Execute: ver /?
Displays the Windows version.

VER
14:04:08,05 Execute: echo content !var!
content content

已编辑:根据 sst 的建议,我删除了辅助函数,但为了能够使用自定义且漂亮的前缀,我决定不使用宏中的 echo on

:echo
    @echo on
    %*
    @set __echo_errorlevel=%ERRORLEVEL%
    @echo off
exit /b %__echo_errorlevel%

call :echo command arg1 arg2 arg3...一样使用它。 (如果你调用 :echo@echoon,这将把它变成 off。)