简体   繁体   English

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

[英]Turn on echo for a single command in a batch file

In a Windows Batch file, if I had echoing enabled (the default), I could disable echoing an individual command by prefixing an @ , eg:在 Windows 批处理文件中,如果我启用了回显(默认设置),我可以通过添加前缀@来禁用回显单个命令,例如:

some_command_that_will_be_echoed
@some_command_that_wont_be_echoed

However, I have echoing disabled with @echo off , but from time to time I would like to echo the command.但是,我使用@echo off禁用了回显,但有时我回显命令。 Is there some magic equivalent?有一些神奇的等价物吗? eg:例如:

@echo off
some_command_that_wont_be_echoed
<unknown_magic_prefix> some_command_that_will_be_echoed

I found this question: turn on echo temporarily , but that essentially says 'turn echo on, do your thing, then turn it off again'.我发现了这个问题: 暂时打开回声,但这实际上是说“打开回声,做你的事,然后再把它关掉”。 I could do that, but I'm hoping for something more elegant.可以做到,但我希望有更优雅的东西。

EDIT: this Windows batch file: how to enable inline echo of a command turned up in the releated questions list;编辑:这个Windows 批处理文件:如何启用相关问题列表中出现的命令的内联回显; it is basically the same question as mine, and about as successful!这基本上和我的问题一样,而且差不多成功了!

EDIT2: in the interests of providing context, below is a section of what I'm up to. EDIT2:为了提供上下文,下面是我所做的一部分。 Note that I've adorned it with C-style comments for the benefit of this post;请注意,为了这篇文章的方便,我用 C 风格的注释装饰了它; they're not in the real script.他们不在真正的剧本中。

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

But just to emphasise, while this is a specific example which would be relatively easy to 'fix', I'm after a more generic solution that I could use in other situations that might not be so simple.但只是为了强调,虽然这是一个相对容易“修复”的特定示例,但我正在寻找一个更通用的解决方案,我可以在其他可能不那么简单的情况下使用它。

@Compo reasonably pointed out that @echo off is not very elegant in itself. @Compo 有理有据地指出,@echo @echo off本身并不是很优雅。 That's fair, but a) it's already enabled in this case and b) it's pretty idiomatic.这很公平,但是 a) 在这种情况下它已经启用并且 b) 它非常惯用。 I like the clarity of this (imaginary syntax):我喜欢这个(虚构的语法)的清晰度:

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

versus相对

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

Clearly that's a subjective opinion, but boils down to a desire to decorate only the uncommon case.显然这是一个主观意见,但归结为只装饰不常见的情况的愿望。

And finally, if it's really not possible, then fair enough, but there's no harm in being sure :)最后,如果真的不可能,那也很公平,但确定一下也没什么坏处:)

You can use a small macro ( %+@% ), it will show the command and then executes it.您可以使用一个小宏( %+@% ),它将显示命令然后执行它。

@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

Outputs:输出:

14:04:08,05 Execute: ver 14:04:08,05 执行:ver

Microsoft Windows [Version 10.0.17134.1069]微软 Windows [版本 10.0.17134.1069]
14:04:08,05 Execute: ver /? 14:04:08,05 执行:版本/?
Displays the Windows version.显示 Windows 版本。

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

Edited: From the suggestions of sst, I removed the helper function, but to be able to use a custom and nice prefix, I decided against the echo on inside the macro.编辑:根据 sst 的建议,我删除了帮助程序 function,但为了能够使用自定义和漂亮的前缀,我决定反对宏内部的echo on

Here's an example which may help you:这是一个可以帮助您的示例:

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

Use this like call:echo command arg1 arg2 arg3... .call:echo command arg1 arg2 arg3...一样使用它。 (If you call :echo while @echo is on , this will turn it off .) (如果你在@echo on时调用:echo ,它会被off 。)

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

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