繁体   English   中英

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

[英]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

我发现了这个问题: 暂时打开回声,但这实际上是说“打开回声,做你的事,然后再把它关掉”。 可以做到,但我希望有更优雅的东西。

编辑:这个Windows 批处理文件:如何启用相关问题列表中出现的命令的内联回显; 这基本上和我的问题一样,而且差不多成功了!

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 @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 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

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

版本
14:04:08,05 执行:回显内容!var!
内容内容

编辑:根据 sst 的建议,我删除了帮助程序 function,但为了能够使用自定义和漂亮的前缀,我决定反对宏内部的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
)
:echo
    @echo on
    %*
    @set __echo_errorlevel=%ERRORLEVEL%
    @echo off
exit /b %__echo_errorlevel%

call:echo command arg1 arg2 arg3...一样使用它。 (如果你在@echo on时调用:echo ,它会被off 。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM