I've a batch file which takes couple of input by prompting at the shell. The content of the batch file named 'askquestion.bat' is given below:
@echo off
:step1
set A1=
set A2=
set /a COUNT=1
:step2
set /p A1=What is your name?
echo Hi %A1%,
:step3
set /p A2=How old are you?
if "%A2%"=="" set A2=0
set /a A2=1+("%A2%")-1 > nul 2>&1
if not ERRORLEVEL 0 set A2=0
if /i "%1"=="noval" goto :step4
if %A2% LEQ 0 (
echo %A1%, '%A2%' may not be a valid age, please try again.
set /a COUNT+=1
if %COUNT% GEQ 5 goto :step4
goto :step3
)
:step4
if %A2% LEQ 10 echo Welcome kid
echo Name:%A1%, Age:%A2%
echo bye
set A1=
set A2=
set COUNT=
It prompts for two inputs - name and age. But the age could be validated and prompted more than once if required. For avoiding infinite loop, the validation will be done for 5 wrong retries.
The common attempt to automate the input would be though using pipe. For single input, we can use echo
c:\>echo Thilak | askquestion.bat
This does not give input for second question and so the script runs on the unfinished (not sure what state it will be) pipe without input causing the script to run 5 retries (or infinitely without that retries check).
How to give multiple input?
Attempt 1:
c:\>echo Thilak\n35 | askquestion.bat
Attempt 2:
c:\>for %a in (Thilak 35) do echo %a | askquestion.bat
Attempt 3:
Create a file with answers in each line and save with a name answer.txt
c:\>type answer.txt | askquestion.bat
Attempt 4:
c:\>echo Thilak & echo 32 | askquestion.bat
Attemp 5:
c:\>(echo Thilak & echo 32) | askquestion.bat
Most of the common approaches failed. This mean to me that there it is a challenge for me. I took it up and started working on it. Google-ing and forum postings did not give answer to the specific scenario.
Dragged further by the attempt 3, I used dir instead of type. Luckily there were lots of files in that folder and my first observation itself gave me a clue.
c:\>dir | askquestion.bat
The important observation is that, the script received it's second input. And the input was taken after 1023 characters. So, this number is a well know one and I was able to do few assumptions quickly that the DOS shell input buffer is of size 1024 and the last character has to be NULL '\0' and so, 1023 character can be taken as input and flushed to receive the next set of inputs.
Now I started changing my answer.txt to have each answer to be of 1023 characters in each line. Line should be terminated with \n (\r\f or [cr][lf]). Basically I used spaces as the filler and it reflected immediately as a wrong choice. I should have used '\0' (NULL) as the fillers, but how? Then I tried another approach - First line with the actual data followed with dummy lines to fill the rest of the 1023 bytes and then the next actual data. During this, I realized that the last answer does not require to follow with the filler.
This was hard to get automated and did not spend much time in using the shell commands. I used to do Perl scripting and thought of using it here. The file name is 'ansquestion.pl'.
my @ans=("Thilak", "10");
open(QUESTION, "|cmd.exe /c askquestion.bat") or die "psht! problem while open.\n";
foreach(@ans){
$|=1;
print QUESTION pack("a1023", ($_, "\n"));
}
close(QUESTION) or die "problem while close";
The Perl pack() method was very handy to solve the filler issue. In the above code, the pack method creates a character byte stream of length 1023 filled with the list of arguments and remaining bytes with NULL. Following command would fulfill my requirement.
c:\>ansquestion.pl | askquestion.bat
Hopefully, someone can find a batch file to generate this output using for, if and echo commands.