Page 1 of 1

Find the ECHO state

Posted: 08 May 2011 05:17
by WernerGg
During debugging with "echo on" I want to disable echo within uninteresting subfunctions and reestablish the original state later. I.e something like:

Code: Select all

call :EchoOff
:: do uninteresting things
call :EchoBack

I try:

Code: Select all

:EchoOff
:: Store current echo-state. Switch echo off
set $LEchoOn=off&for /f %%o in ('echo|find "(ON)"') do (set $LEchoOn=on)
echo off
goto :EOF

:EchoBack
:: Restore previous echo-state.
echo %$LEchoOn%
goto :EOF


But the echo|find combination does not work.

I try manually at the command promt (red my entries):
>echo
ECHO ist eingeschaltet (ON).

>echo | find "(ON)"
ECHO ist eingeschaltet (ON).

>echo off
echo
ECHO ist ausgeschaltet (OFF).
echo | find "(ON)"
ECHO ist eingeschaltet (ON).


I am stuck at the last line. echo is definitly off, but echo | find "(ON)" finds it ON.

??

Re: Find the ECHO state

Posted: 08 May 2011 06:24
by aGerman
Ha ha, you're right. ECHO and | result in awrong output. No idea why. Later I'll try to solve it. Currently I'm at work.

Regards
aGerman

Re: Find the ECHO state

Posted: 08 May 2011 08:02
by WernerGg
??? :roll:

This works:
>echo
ECHO ist eingeschaltet (ON).

>echo>e.tmp & find "(ON)" e.tmp

---------- E.TMP
ECHO ist eingeschaltet (ON).

>echo off
echo
ECHO ist ausgeschaltet (OFF).
echo>e.tmp & find "(ON)" e.tmp

---------- E.TMP


But I don't like that useless e.tmp file. And I must get rid of that first header line from find.

Re: Find the ECHO state

Posted: 08 May 2011 08:16
by aGerman
Found no better way than redirecting the output to a file. Have a look at my solution:

Code: Select all

@echo off &setlocal
echo 1st Hello World
call :switchEchoState
echo 2nd Hello World
call :switchEchoState
echo 3rd Hello World

pause
goto :eof

:switchEchoState
@prompt $ &>"%temp%\state.tmp" echo
@echo off
for /f %%a in ('findstr /i "off" "%temp%\state.tmp"^&del "%temp%\state.tmp"') do (prompt &echo on &goto :eof)
prompt
goto :eof


Regards
aGerman

Re: Find the ECHO state

Posted: 08 May 2011 10:17
by WernerGg
OKI, so we seem to have a strange bug in echo piped to find.

I did not understand the prompt-game in your code. (Btw: another undocumented feature "prompt $").

I do not want to toggle echo-state, but
    - switch it temporarily off (or on) - normaly after entry into a subroutine
    - and then back to previous state. - normaly before exit from that subroutine

I do that with one simple variable $EchoState. To be perfect I needed a stack for echo-state instead. Then one could switch echo locally on or off during a call chain.

What I finally use now:

Code: Select all

rem ##----------------------------------------------------------
:EchoOnOff on/off
:: Store current echo-state. Switch echo on or off.
::   on/off : Desired new echo state
::The following does not work. Bug in echo|find
::@set $LEchoState=off&for /f %%o in ('echo^|find "(ON)"') do (set $LEchoState=on)
:: Determine current echo-state
@set $tmp="%temp%\_gd_echostate.tmp"
@echo >%$tmp%
@set $LEchoState=off&for /f %%a in ('findstr "(ON)" %$tmp%') do (@set $LEchoState=on)
@del %$tmp%
:: Switch echo on/off as requested
@echo %~1
::@echo EchoOnOff: echo now temporarily %~1
Exit /b

rem ##----------------------------------------------------------
:EchoBack
:: Restore previous echo-state as stored by EchoOnOff
@echo %$LEchoState%
::@echo EchoBack: echo now back to %$LEchoState%
Exit /b


Regards,
Werner

Re: Find the ECHO state

Posted: 09 May 2011 04:34
by amel27
WernerGg wrote:OKI, so we seem to have a strange bug in echo piped to find.
it is not bug, it is by disign
command to the left of a pipe run in separate CMD.EXE process, it is easy for checking up in task manager:

Code: Select all

@echo off
(
echo
pause>nul
)|find /v ""
Thus, echo display status of other CMD process, not current.

Re: Find the ECHO state

Posted: 10 May 2011 00:32
by WernerGg
@amel27
Aha! Thank you for that explanation. I did not know that but it is clear now.

So the trick would be to somehow capture the stdout from "echo" without starting a new command processor.