Edd*_*lis 5 windows batch-file echo
在 Windows 批处理文件中,如果我启用了回显(默认),我可以通过添加前缀来禁用回显单个命令@
,例如:
some_command_that_will_be_echoed
@some_command_that_wont_be_echoed
Run Code Online (Sandbox Code Playgroud)
但是,我已禁用回显@echo off
,但有时我想回显该命令。有什么神奇的等价物吗?例如:
@echo off
some_command_that_wont_be_echoed
<unknown_magic_prefix> some_command_that_will_be_echoed
Run Code Online (Sandbox Code Playgroud)
我发现了这个问题:暂时打开回声,但这基本上是说“打开回声,做你的事,然后再次关闭它”。我可以这样做,但我希望有更优雅的东西。
编辑:此Windows 批处理文件:如何启用相关问题列表中出现的命令的内联回显;这基本上和我的问题一样,而且成功了!
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.
Run Code Online (Sandbox Code Playgroud)
但只是强调一下,虽然这是一个相对容易“修复”的特定示例,但我正在寻找一个更通用的解决方案,我可以在其他可能不那么简单的情况下使用它。
@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
Run Code Online (Sandbox Code Playgroud)
相对
@command_1 // not echoed
@command_2 // not echoed
command_3 // IS echoed and gets a little lost
@command_4 // not echoed
Run Code Online (Sandbox Code Playgroud)
显然,这是一种主观意见,但归结为只装饰不常见情况的愿望。
最后,如果真的不可能,那就足够公平了,但确定没有坏处:)
您可以使用一个小宏(%+@%
),它会显示命令然后执行它。
@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
Run Code Online (Sandbox Code Playgroud)
输出:
14:04:08,05 执行:ver
Microsoft Windows [版本 10.0.17134.1069]
14:04:08,05 执行:ver /?
显示 Windows 版本。VER
14:04:08,05 执行: echo content !var!
内容内容
编辑:根据 sst 的建议,我删除了辅助函数,但为了能够使用自定义且漂亮的前缀,我决定反对echo on
宏内部。