Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why this code says echo is off?

What is wrong with this code? It says ECHO is off.

@ECHO off set /p pattern=Enter id: findstr %pattern% .\a.txt > result if %errorlevel%==0 ( set var2= <result echo %var2% set var1=%var2:~5,3% echo %var1% > test.txt echo %var1% ) else ( echo error ) del result pause 

Any help is appreciated.

like image 539
user1979801 Avatar asked Jan 15 '13 09:01

user1979801


People also ask

Why am I getting echo is off?

If your variable is empty somewhere, it will be the same as having the command "echo" on its own, which will just print the status of echo. That way, if %var2% is empty it will just print "echo var2:" instead of "echo off".

What does echo off mean in a batch file?

When echo is turned off, the command prompt doesn't appear in the Command Prompt window. To display the command prompt again, type echo on. To prevent all commands in a batch file (including the echo off command) from displaying on the screen, on the first line of the batch file type: Copy. @echo off.

What is echo off and echo on?

The ECHO-ON and ECHO-OFF commands are used to enable and disable the echoing, or displaying on the screen, of characters entered at the keyboard. If echoing is disabled, input will not appear on the terminal screen as it is typed. By default, echoing is enabled.

How do I get out of echo command?

The echo command ends with a line break by default. To suppress this, you can set the control character \c at the end of the respective output.


2 Answers

If your variable is empty somewhere, it will be the same as having the command "echo" on its own, which will just print the status of echo.

To avoid this, you should replace all your echo commands with something like this:

echo var2: %var2% 

That way, if %var2% is empty it will just print "echo var2:" instead of "echo off".

like image 104
laurent Avatar answered Oct 03 '22 05:10

laurent


As Laurent stated, it's not a problem of the ECHO, it's a problem of your code.

In batch files, blocks are completely parsed before they are executed.
While parsing, all percent expansion will be done, so it seems that your variables can't be changed inside a block.

But for this exists the delayed expansion, the delayed expansion will be evaluated in the moment of execution not while parsing the block.

It must be enabled, as per default the delayed expansion is disabled.

@ECHO off setlocal EnableDelayedExpansion set /p pattern=Enter id: findstr %pattern% .\a.txt > result if %errorlevel%==0 (   set var2= <result   echo(!var2!   set var1=!var2:~5,3!   echo(!var1! > test.txt   echo(!var1! ) else (   echo error ) del result 

I used here the construct echo( instead of echo as this will ensure echoing an empty line even if the variable is empty.

like image 44
jeb Avatar answered Oct 03 '22 06:10

jeb