Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn on echo for a single command in a batch file

In a Windows Batch file, if I had echoing enabled (the default), I could disable echoing an individual command by prefixing an @, eg:

some_command_that_will_be_echoed
@some_command_that_wont_be_echoed

However, I have echoing disabled with @echo off, but from time to time I would like to echo the command. Is there some magic equivalent? eg:

@echo off
some_command_that_wont_be_echoed
<unknown_magic_prefix> some_command_that_will_be_echoed

I found this question: turn on echo temporarily, but that essentially says 'turn echo on, do your thing, then turn it off again'. I could do that, but I'm hoping for something more elegant.

EDIT: this Windows batch file: how to enable inline echo of a command turned up in the releated questions list; it is basically the same question as mine, and about as successful!

EDIT2: in the interests of providing context, below is a section of what I'm up to. Note that I've adorned it with C-style comments for the benefit of this post; they're not in the real script.

@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.

But just to emphasise, while this is a specific example which would be relatively easy to 'fix', I'm after a more generic solution that I could use in other situations that might not be so simple.

@Compo reasonably pointed out that @echo off is not very elegant in itself. That's fair, but a) it's already enabled in this case and b) it's pretty idiomatic. I like the clarity of this (imaginary syntax):

@echo off
command_1    // not echoed
command_2    // not echoed
+@command_3  // IS echoed and stands out
command_4    // not echoed

versus

@command_1    // not echoed
@command_2    // not echoed
command_3     // IS echoed and gets a little lost
@command_4    // not echoed

Clearly that's a subjective opinion, but boils down to a desire to decorate only the uncommon case.

And finally, if it's really not possible, then fair enough, but there's no harm in being sure :)

like image 286
Edd Inglis Avatar asked Nov 06 '19 14:11

Edd Inglis


1 Answers

You can use a small macro (%+@%), it will show the command and then executes it.

@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

Outputs:

14:04:08,05 Execute: ver

Microsoft Windows [Version 10.0.17134.1069]
14:04:08,05 Execute: ver /?
Displays the Windows version.

VER
14:04:08,05 Execute: echo content !var!
content content

Edited: From the suggestions of sst, I removed the helper function, but to be able to use a custom and nice prefix, I decided against the echo on inside the macro.

like image 50
jeb Avatar answered Sep 28 '22 05:09

jeb