Which commands (and how) fail when pipe is used.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
npocmaka_
Posts: 495
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Which commands (and how) fail when pipe is used.

#1 Post by npocmaka_ » 14 Aug 2015 17:01

Just a simple test because I wondered what else fails. I think it does not matter if pipe is before or after these commands.And they fail the same way when are processed by FOR /F .

here's the test with I've found (putting them after the pipe makes easier the output tracking)

Code: Select all

@echo off
cls
echo start
set "checkline=if defined # echo #defined#"


break|(
      
   :: set works but to access the variable call is needed
   set "#=#"
   ( %%checkline%% )
   echo -%#%-
   call echo -%%#%%-
   
   :: shift is not working
    shift
   echo %~n0
   
   :: echo state is changed
   echo

   
   :: setlocal and endlocal are not working
   setlocal enableDelayedExpansion
   endlocal
   
   
   :: pushd /popd are not working
   pushd c:\
   call echo  %%cd%%
   
   
   :: call/goto label are not working
   goto :end
   echo marker
   
   :: cd also
   cd c:\
   call echo %%cd%%
   
   :: prompt is not working (but title does)
   prompt ###
   call echo %%prompt%%
   
   ::path/dpath are not working
   path .
   call echo "%%path%%"
   
   :: pause is not pausing
   pause
   
   :: date/time does not wait for input
   date
   time

)
:end
echo end

rem is working strangely when is the last command - it remarks  everything
break|(
   rem
   echo -+-+-+-
   rem
)

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Which commands (and how) fail when pipe is used.

#2 Post by Yury » 15 Aug 2015 06:25

Hi, npocmaka_!

Do not use only the commands that do not work in the command prompt such as

    SHIFT
    SETLOCAL
    ENDLOCAL
    GOTO label
    CALL :label

and also

    REM

.

The rest of the commands are working under certain conditions of using.

Test this code:

Code: Select all

@echo off
echo start
echo.
call :# argument
exit/b


:#
break|(

   echo Testing of "SET":
   set "#=#"
   cmd/c if defined # echo #defined#
   echo -%#%-
   call echo -%%#%%-
   cmd/v/c echo -!#!-
   echo.

   echo Testing of "SHIFT":
   echo %0 %1
   shift
   echo %0 %1
   echo.

   echo Testing of "ECHO":
   echo
   echo.

   echo Testing of "SETLOCAL" and "ENDLOCAL":
   setlocal enabledelayedexpansion
   set "var=$"
   echo !var!
   call echo %%var%%
   endlocal
   call echo %%var%%
   echo.

   echo Testing of "PUSHD" and "POPD":
   pushd C:\
   cd
   cmd/v/c echo !cd!
   popd
   cd
   cmd/v/c echo !cd!
   echo.

   echo Testing of "CD":
   cd /d C:\
   cd
   cmd/v/c echo !cd!
   echo.

   echo Testing of "PROMPT":
   prompt ###
   cmd/v/c echo !prompt!
   echo.

   echo Testing of "PATH":
   path .
   path
   calc.exe
   notepad.exe
   cmd.exe
   echo.

   echo Testing of "PAUSE":
   <con pause
   echo.

   echo Testing of "DATE" and "TIME":
   <con date
   <con time
   echo.
   echo.

   echo Testing of "CALL" and "GOTO":
   call :end
   goto end
   echo marker
   echo.

)


:end
echo end
pause


.
Last edited by Yury on 16 Aug 2015 11:14, edited 2 times in total.

Aacini
Expert
Posts: 1661
Joined: 06 Dec 2011 22:15
Location: México City, México

Re: Which commands (and how) fail when pipe is used.

#3 Post by Aacini » 15 Aug 2015 07:00

Well, the keypoint here is that a pipe cause to execute the commands placed at both sides of the pipe in the command-line context, not the Batch-file context. At this topic I posted a list of the commands that behaves differently in this case; this is a summary:

  • Batch-file parameters can not be used nor SHIFT command.
  • @ECHO OFF command just supresses the echoing of the prompt and FOR iterations.
  • SETLOCAL/ENDLOCAL commands have not effect. You may use the CALL trick to expand changing variables. There is an implicit SETLOCAL because each side of the pipe is executed as a child process.
  • SET /P command does not read data from the keyboard, but from the lines placed after it. The same happen with PAUSE command (or any command that read data).
  • GOTO and CALL commands does not work on labels. However, an external Batch file may be invoked with or without the CALL command with the same result, even inside IF or FOR.
  • EXIT or EXIT /B commands terminate the execution.

Antonio

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Which commands (and how) fail when pipe is used.

#4 Post by Yury » 15 Aug 2015 09:02

Aacini wrote:
  • SET /P command does not read data from the keyboard, but from the lines placed after it. The same happen with PAUSE command (or any command that read data).




Yes!

But:

Code: Select all

@break| <con (

set/p "var=Enter the value of the variable: "

cmd/c if defined var call echo "%%var%%"
:: or
cmd/v/c if defined var echo "!var!"

pause

)


!

Aacini
Expert
Posts: 1661
Joined: 06 Dec 2011 22:15
Location: México City, México

Re: Which commands (and how) fail when pipe is used.

#5 Post by Aacini » 15 Aug 2015 09:13

Yes. From the same post I linked:

Aacini wrote:To read data from keyboard, execute: "set /P var=Prompt: < CON > CON".


An from this SO post:

@echo off
rem You may force SET /P command to read the line from keyboard instead of
rem from following lines by redirecting its input to CON device.

rem You may also use CON device to force commands output to console (screen),
rem this is easier to write and read than >&2


Antonio

npocmaka_
Posts: 495
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: Which commands (and how) fail when pipe is used.

#6 Post by npocmaka_ » 15 Aug 2015 09:57

Clever trick with the cmd/c.

One more command that needs it (and I almost forgot about):

Code: Select all

keys 
keys on
break|(
  keys
  keys off
  cmd/c keys
)

keys



Though! Everywhere where cmd is required the jeb's trick with double expansion can be used - ant it will be faster:
@echo off

Code: Select all

set ks=keys
keys
keys on

break|(
   keys off
   ( %%ks%% )
)

Post Reply