在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
显然,这是一个主观意见,但归结为只装饰罕见的案件的愿望。,那就足够公平了,但是最后,如果不可能[[really
sure并没有害处:)
@(
Echo On
Title CMD Window with SVN enabled
Set "PATH=%PATH%;D:\ummy\directory"
)
svn --version --quiet
@(
Rem All other none echoed commands here
Pause
Echo Off
Exit /B
)
@echo off
call :define_macro
%+@% svn --version --quiet
%+@% ver /?
exit /b
:define_macro
(set \n=^^^
)
set ^"+@=for %%# in (1 2) do if %%#==2 (%\n%
echo !time: =0! Execute: !argv!%\n%
for /F "tokens=*" %%1 in ("!argv!") do (%\n%
endlocal %\n%
%%1%\n%
)%\n%
) ELSE setlocal EnableDelayedExpansion ^& set argv="
exit /b
编辑:从sst的建议中,我删除了helper函数,但是为了能够使用自定义的漂亮前缀,我决定反对宏中的echo on
。