0

I got a script that asks 1000 times for input of 1-5, it looks like this:

insert1:

insert2:

insert3:

insert4:

insert5: //and again 1-5

insert 1: ...in total it will get 1000 inputs

I want to write a one line script that will run the script I just described, it will insert the input that needed each time. this is what I tried:

#!/bin/bash
./my_script.exe  -l | for i in {1..200}; do for j in {1..5}; do j; done; done
1
  • 1
    Remember, a pipe connects the output of the thing on the left to the input of the thing on the right. (And j just runs the command j, it doesn't have anything to do with the contents of the variable expanded with $j). Commented Jan 4, 2024 at 16:31

1 Answer 1

1

You are nearly there, but do it the other way around:

for i in {1..200} ; do
    for j in {1..5} ; do
        echo $j
    done
done | ./myscript.exe -l

You can put a # before the | to comment it out and see what the script sends to your program.


You need to differentiate between parameters which are specified after the program name like this:

program param1 param2 param3

and inputs, which a program gets by reading its stdin and are supplied like this:

printf "input1\ninput2\ninput3\n" | program

Alternative version of second command:

{ echo input1; echo input2; echo input3; } | program
Sign up to request clarification or add additional context in comments.

1 Comment

@Rojo There's nothing wrong with your edit per se; but there also was nothing wrong with the original code. If you have a reason for changing the for loops, can you please share it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.