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 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
Microsoft Windows [バージョン10.0.17134.1069]
14:04:08,05実行:ver /?
Windowsのバージョンを表示します。VER
14:04:08,05実行:echo content!var!
コンテンツコンテンツ
編集:sstの提案から、ヘルパー関数を削除しましたが、カスタムで素敵なプレフィックスを使用できるようにするためecho on
に、マクロの内部に反対することにしました。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加