Question How to pipe output of some command to batch function?

Sep 7, 2020
13
0
10
2
Hello! I try to write Batch script similar to Bash one (open in Repl.it):
Bash:
function f()
{
  read foo
  echo "$foo"
}

echo "arg" | f
I've written it:
Code:
@echo off
    echo arg|call :f
    pause
@echo on
@goto :eof

:f
    set /p foo=""
    echo %foo%
exit /b 0

:eof
, but this variant doesn't work with following error:
Invalid attempt to call batch label outside batch script.
What's wrong with it?
 
Last edited:
Sep 7, 2020
13
0
10
2
I've found that .bat files could be used instead of functions to pipe values to. In my task it is not critical to use them instead of functions.

For instance we have two Batch Scripts:
Code:
:: 1.bat
echo off
echo Test | 2.bat
pause
echo on
Code:
:: 2.bat
echo off
set /p dummy=
echo Piped value=%dummy%
pause > nul
echo on
When 1.bat is run Piped value=Test output is shown.
 

ASK THE COMMUNITY