I want to run a program that expects two inputs after executing to decide how it will run. Like so:

$ program <input_file>
Running program
1. Option one
2. Option two
select an option: <Expects `1` or `2`>

3. Option three
4. Option four
select an option: <Expects `3` or `4`>

Running with selected options.

I would like to run it for many input files for which the same options will be selected. Right now I do:

$ for i in *; do program $i; done

With this I have to select the options manually for every file. How can I pass the options to the program to automate it? Something like:

$ for i in *; do program $i < <options>; done

The program repeats the available options if the correct one is not selected. So when I tried with {echo 2; echo 4} and echo "2\n4\n" it gets stuck on the first option selection.


One option is to Elseif everything

IF "%ID%"=="0" ( ... & ... & ... ) ELSE ^
IF "%ID%"=="1" ( ... ) ELSE ^
IF "%ID%"=="2" ( ... ) ELSE ^
REM default case...

Second option

echo Choose from the list given below:
echo [1] Option 1
echo [2] Option 2
echo [3] Option 3

set /a one=1
set /a two=2
set /a three=3
set /a four=4
set input=
set /p input= Enter your choice:
if %input% equ %one% goto Z if NOT goto Start2
if %input% equ %two% goto X if NOT goto Start2
if %input% equ %three% goto C if NOT goto Start2
if %input% geq %four% goto N

echo You have selected : Option 1
set option=Option 1
echo %year%
call:branches option



StackOverflow thread they have more knowledge in batch and programming language

  • Does anyone knows about a SWITCH CASE or SELECT CASE in batch ? – DIDIx13 Feb 8 '18 at 16:18
  • I'm really confused by your answer. Maybe because I wasn't clear with my redaction. I want to use a bash script to interact with the program without me having to select the options (I don't want to type them every time I run it). – izxle Feb 8 '18 at 16:54
  • Sorry if I misunderstood you, so to clarify, you want a script who runs ask the user to choose between some options and run the code from the selected option? If I'm still wrong can you maybe do a programming flowchart? – DIDIx13 Feb 9 '18 at 7:08

when I tried with {echo 2; echo 4} and echo "2\n4\n" it gets stuck on the first option selection.

It's not clear to me how you tried this, but:

  • {echo 2; echo 4} is syntactically invalid; you want { echo 2; echo 4; };
  • echo "2\n4\n" just prints 2\n4\n; you want echo -ne "2\n4\n" (note: various implementations of echo may behave differently, but since echo is a builtin in bash and you tagged , I assume you use the builtin).

I would say this might work:

for i in *; do { echo 2; echo 4; } | program "$i"; done

I can imagine program silently depletes and discards its stdin before it asks for options, or uses another trick that makes the above solution fail. In this case try expect, if available.

man 1 expect states:

expect is a program that "talks" to other interactive programs according to a script. Following the script, expect knows what can be expected from a program and what the correct response should be.


In general, expect is useful for running any program which requires interaction between the program and the user. All that is necessary is that the interaction can be characterized programmatically.

This example sends 2 and 4 when the program asks for them:

expect -c '
        spawn program ./input_file
        expect "2. Option two"
        expect "select an option:*"
        send "2\r"
        expect "4. Option four"
        expect "select an option:*"
        send "4\r"
        expect eof'

Using this with shell variables requires proper quoting and escaping:

for i in *; do
  expect -c "
        spawn program \"$i\"
        expect \"2. Option two\"
        expect \"select an option:*\"
        send \"2\r\"
        expect \"4. Option four\"
        expect \"select an option:*\"
        send \"4\r\"
        expect eof"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.