在批处理文件中为单个命令打开echo

在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,但这实际上是在说“打开回声,执行您的操作,然后再次将其关闭”。我可以做到这一点,但我希望有一些更优雅的东西。

编辑:此Windows batch file: how to enable inline echo of a command在相关问题列表中出现;这基本上和我的问题相同,而且差不多成功!

EDIT2:为了提供上下文,下面是我要介绍的部分。请注意,为使本博文受益,我已使用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.

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

@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

很明显,这是一个主观意见,但归结为只装饰罕见的案件的愿望。

最后,如果真的不可能,那么还算公平,但是确保确定并没有什么害处:)

ahricher1 回答:在批处理文件中为单个命令打开echo

您可以使用一个小宏(%+@%),它将显示命令并执行。

@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执行:ver

     

Microsoft Windows [版本10.0.17134.1069]
  14:04:08,05执行:ver /?
  显示Windows版本。

     

VER
  14:04:08,05执行:echo content!var!
  内容

编辑:从sst的建议中,我删除了helper函数,但是为了能够使用自定义且美观的前缀,我决定反对宏内的echo on

,

以下是一个可以帮助您的示例:

@(
    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
)
本文链接:https://www.f2er.com/3151240.html

大家都在问