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

标签 windows batch-file 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出现在相关问题列表中;这基本上和我的问题相同,而且也同样成功!

编辑2:为了提供上下文,下面是我正在做的部分。请注意,为了这篇文章的利益,我用 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

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

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

最佳答案

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

@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

关于windows - 打开批处理文件中单个命令的回显,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58732724/

相关文章:

linux - Bash 脚本打印错误的值

javascript - 如何将 PHP 放入 Javascript 中?

bash - 将整个文件读取到变量,保持转义字符完整

windows - 批量 |回显多个变量到文件

windows - 在Win 10上访问Docker卷

windows - Ant BuildException 错误构建 Hadoop 2.4.1

windows - 在awk中转义嵌套的双引号

windows - 将 Windows 批处理文件变量设置为星期几

batch-file - 如何修复 Windows 批处理文件 FOR 命令返回 "Active code page: 65001"

mysql - MySQL 服务器问题