Combining multiple values ​​into a program in batch script

I am writing my own simple system to automatically sign APKs before they are uploaded to GPlay. I have a batch file that performs signing; and the wrapper batch file, the contents of which will be run on the Jenkins post-build command line.

sign_apks.bat:

@echo off

set /p job= "Enter job name: "
set /p alias= "Enter key alias: "
set /p mobile= "Sign mobile? (y/n): "
set /p wear= "Sign wear? (y/n): "

echo.
echo "%job%"
echo "%alias%"
echo "%mobile%"
echo "%wear%"

[the rest of the code is sensitive so is emitted, but these variables are used later]

shell code:

@echo off

(echo test
echo test
echo y
echo y)| call sign_apks.bat

In this article, I showed how to pass values ​​to a program. To quote an answer:

Multiple lines can be entered like so:

(echo y
echo n) | executable.exe

...which will pass first 'y' then 'n'.

However, this does not work. This is the result that I get when I run the shell code:

Enter job name: Enter key alias: Sign mobile? (y/n): Sign wear? (y/n):
"test "
""
""
""

reference

+4
source share
3 answers

, job, alias, mobile wear script , stdin. set /p, , .

@echo off
setlocal

set "job=%~1"
set "alias=%~2"
set "mobile=%~3"
set "wear=%~4"

if not defined job set /p "job=Enter job name: "
if not defined alias set /p "alias=Enter key alias: "
if not defined mobile set /p "mobile=Sign mobile? (y/n): "
if not defined wear set /p "wear=Sign wear? (y/n): "

echo.
echo "%job%"
echo "%alias%"
echo "%mobile%"
echo "%wear%"

, call sign_apks.bat, :

call sign_apks.bat test test y y
+1

- , SET/P , .

: -)

- , .

@echo off
(
  echo test
  echo test
  echo y
  echo y
)>responses.temp
call sign_apks.bat <responses.temp
delete responses.temp

. ( ). , , .

, , .

@echo off
(
  call echo test1
  call echo test2
  call echo y1
  call echo y2
) | sign_apks.bat

- -

Enter job name: Enter key alias: Sign mobile? (y/n): Sign wear? (y/n):
"test1 "
"test2 "
"y1 "
"y2 "

, CALL SET/P . , . , CALL , script .

cmd.exe. , , :

C:\Windows\system32\cmd.exe /S /D /c" sign_apks.bat"

CALL - cmd.exe.

, . , CMD.EXE/C. CMD.EXE , . , . , - :

C:\Windows\system32\cmd.exe /S /D /c" ( call echo test & call echo test & call echo y & call echo y )"

, . . , ? , .

- . script, WriteArgs.bat, ECHO .

WriteArgs.bat

@echo off
:loop
if .%1 equ . exit /b
echo %1
shift /1
goto loop

script , :

WriteArgs.bat test test y y | sign_apks.bat

, , SET/P , . : -)

. , . , , , , - . , , - , .

+3

set /p, , , , , .

echo, set/p, set/p, .

dbenham , !
( set/p) cmd.exe, , .

But you can ensure proper consumption by sharing content with another program, such as moreor findstr.
Because they crush the input buffer at the line boundaries.

+1
source

Source: https://habr.com/ru/post/1568762/


All Articles