Page 1 of 1

@Echo off is not working when Piping batch code to cmd

Posted: 15 Mar 2020 03:58
by BoQsc
I would like to know if there is any known way to turn off Echo when piping batch code to cmd.

Only by adding /Q option to cmd.exe, the output is actually with echo off.

I need the script to be the trigger of echo off, and not the /Q option of cmd.exe

If not, Are there any alternative ways to execute code from standard output by piping except for cmd?


Working example:

Code: Select all

curl vaido.world | cmd
Output:

Code: Select all

title New
echo "yessf"
"yessf"
cmd with /Q option

Code: Select all

curl vaido.world | cmd /Q
Output:

Code: Select all

"yessf"

Re: @Echo off is not working when Piping batch code to cmd

Posted: 15 Mar 2020 04:18
by ShadowThief
Why don't you want to use cmd /Q? That's how you do it.

Re: @Echo off is not working when Piping batch code to cmd

Posted: 15 Mar 2020 04:28
by BoQsc
ShadowThief wrote:
15 Mar 2020 04:18
Why don't you want to use cmd /Q? That's how you do it.
You see typing additional options are hard.
They are hard to remember,
takes aditional time for beginners that are only starting to use command line prompt, and they will use this command only few times in years.

I'm starting to think that I should just simply use @ at the beginning of every command.
But that will add more complexity and readability problems.

Re: @Echo off is not working when Piping batch code to cmd

Posted: 15 Mar 2020 04:31
by ShadowThief
Then I suggest you write a wrapper script that takes the URL you're downloading as %1 and pipes that to cmd /q

Re: @Echo off is not working when Piping batch code to cmd

Posted: 15 Mar 2020 04:50
by BoQsc
ShadowThief wrote:
15 Mar 2020 04:31
Then I suggest you write a wrapper script that takes the URL you're downloading as %1 and pipes that to cmd /q
Wouldn't it be possible without wrapper script?
'
There are no ways to enclose code and pipe it into cmd /q like in this pseudo code?

Non-working pseudo-code

Code: Select all

(
echo Hello this is going to be piped to cmd /q
echo everything will work correctly
echo this is only pseudo-code

) | cmd.exe /Q

Uh I quickly came up with poorly looking and almost unreadable but seemingly working example that has no advantage over @ prefixing way.

Code: Select all

@echo off
(
echo @echo off
echo cls
echo echo Hello this is going to be piped to cmd /q
echo echo everything will work correctly
echo echo this is only pseudo-code

) | cmd.exe /q

pause


If only there were no requirement to prefix every command with echo this would be perfect.

And it actually works, however, requires tiring mundane prefixation of echo. Which is sad.

Code: Select all

curl vaido.world/example/ | cmd

Re: @Echo off is not working when Piping batch code to cmd

Posted: 15 Mar 2020 06:06
by ShadowThief
That's fair, if you could put the script on the machine that the code needed to run on, you wouldn't need to download it with curl and pipe it to cmd...

In that case, I can recommend a two-part solution:

Code: Select all

curl vaido.world -o runme.bat && call runme.bat
Or a three-part solution if you want to clean up after yourself:

Code: Select all

curl vaido.world -o runme.bat && call runme.bat && del runme.bat
That's about as close as you're going to get without needing to remember that cmd has a /Q flag.

Re: @Echo off is not working when Piping batch code to cmd

Posted: 16 Mar 2020 01:16
by jeb
Hi BoQs,
BoQsc wrote:
15 Mar 2020 04:50
If only there were no requirement to prefix every command with echo this would be perfect.
There is no need for the ECHO prefix of each line, it's only necessary in your example.

You can download and start a simple script with a pipe.

Test.bat

Code: Select all

@echo off
(
cls

echo Hello
echo Time: !time!
exit

)
And execute it with

Code: Select all

curl -s https://example.com/test.bat | cmd /v:on /k
The code has to follow more or less the rules for batch macros.


A echo off or @ doesn't work, the commands are still displayed, because it's like typing them into cmd.
The only way to hide them is to use /Q or to clear the output with CLS

Re: @Echo off is not working when Piping batch code to cmd

Posted: 16 Mar 2020 04:22
by jeb
If you are on windows10 with ansi escape sequences enabled, you can use some magic to hide the commands.

Code: Select all

cmd /Q /v:on /k
for /F "delims=#" %a in ('"prompt #$E# & for %a in (1) do rem"') do set "ESC=%a" & echo off
<nul set /p ".=%ESC%[G%ESC%[2K%ESC%%ESC%[A%ESC%[2K"
echo #1
echo #2
echo #3

echo !time!
echo ----1
echo ----2
echo ----3
exit

Even without escape sequences, only the first two lines are visible

Re: @Echo off is not working when Piping batch code to cmd

Posted: 17 Mar 2020 15:40
by Aacini
The simplest way is via the cmd /Q trick, but this don't omits the initial text. You may review other differences in the code executed via a .BAT batch file vs. via text redirected into cmd.exe at cmd.exe < NotBatch.txt file topic. For example:

- @ECHO OFF command just supresses the echoing of the prompt and FOR iterations. To supress the echoing of all commands, the NotBatch.txt file must be executed this way: "cmd.exe < NotBatch.txt > NUL" and then use "echo Text > CON" in order to display text in the screen.

Antonio