【发布时间】:2019-11-06 14:48:29
【问题描述】:
在 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 风格的 cmets 装饰了它;它们不在真正的脚本中。
@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
显然这是一种主观意见,但归结为希望只装饰不常见的情况。
最后,如果它真的不可能,那也很公平,但是确定并没有什么坏处:)
【问题讨论】:
-
我们谈论的是批处理文件,没有什么比这更优雅了。您那里的链接就是解决方案。
-
好的,在
some_wrapper_function( command_I_want_echoed )的意义上“更优雅”怎么样,它会将状态恢复到原来的状态,无论是关闭还是打开? -
不要使用
@echo off来全局关闭echoing,它本身就很不雅。使用@前缀,并在必要时将其删除,这是优雅的解决方案。 -
谢谢,@Compo,这是一个非常公平的观点。尽管如此,我发现这会使事情变得混乱。突出特殊情况而普通情况不装饰不是更干净吗?
-
你怎么样edit your question 包含一些真实的代码,然后告诉我们哪些行需要
echoing,哪些不需要。这至少会让我们有机会提供您认为更优雅的解决方案。
标签: windows batch-file echo