[英]Turn on echo for a single command in a batch file
在 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
我發現了這個問題: 暫時打開回聲,但這實際上是說“打開回聲,做你的事,然后再把它關掉”。 我可以做到,但我希望有更優雅的東西。
編輯:這個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.
但只是為了強調,雖然這是一個相對容易“修復”的特定示例,但我正在尋找一個更通用的解決方案,我可以在其他可能不那么簡單的情況下使用它。
@Compo 有理有據地指出,@echo @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 執行:ver
微軟 Windows [版本 10.0.17134.1069]
14:04:08,05 執行:版本/?
顯示 Windows 版本。版本
14:04:08,05 執行:回顯內容!var!
內容內容
編輯:根據 sst 的建議,我刪除了幫助程序 function,但為了能夠使用自定義和漂亮的前綴,我決定反對宏內部的echo on
。
這是一個可以幫助您的示例:
@(
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
@echo on
%*
@set __echo_errorlevel=%ERRORLEVEL%
@echo off
exit /b %__echo_errorlevel%
像call:echo command arg1 arg2 arg3...
一樣使用它。 (如果你在@echo
on
時調用:echo
,它會被off
。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.