在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
很明显,这是一个主观意见,但归结为只装饰罕见的案件的愿望。
最后,如果真的不可能,那么还算公平,但是确保确定并没有什么害处:)