在批处理文件中为单个命令打开echo [英] Turn on echo for a single command in a batch file

查看:96
本文介绍了在批处理文件中为单个命令打开echo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In a Windows Batch file, if I had echoing enabled (the default), I could disable echoing an individual command by prefixing an @, eg:

some_command_that_will_be_echoed
@some_command_that_wont_be_echoed

但是,我已经用@echo off禁用了回显功能,但是我有时会希望响应命令.有魔术吗?例如:

However, I have echoing disabled with @echo off, but from time to time I would like to echo the command. 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.

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

this Windows batch file: how to enable inline echo of a command turned up in the releated questions list; it is basically the same question as mine, and about as successful!

为了提供上下文,下面是我要介绍的部分.请注意,为使本博文受益,我已使用C风格的注释对其进行了修饰.他们不在真实的脚本中.

in the interests of providing context, below is a section of what I'm up to. Note that I've adorned it with C-style comments for the benefit of this post; 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合理地指出@echo off本身并不十分优雅.这很公平,但是a)在这种情况下已经启用,b)很惯用.我喜欢这种(虚构语法)的清晰度:

@Compo reasonably pointed out that @echo off is not very elegant in itself. That's fair, but a) it's already enabled in this case and b) it's pretty idiomatic. 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

@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

输出:

14:04:08,05执行:ver

14:04:08,05 Execute: ver

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

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

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

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

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

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.

这篇关于在批处理文件中为单个命令打开echo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆