Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read user input inside a loop

Tags:

bash

I am having a bash script which is something like following,

cat filename | while read line do     read input;     echo $input; done 

but this is clearly not giving me the right output as when I do read in the while loop it tries to read from the file filename because of the possible I/O redirection.

Any other way of doing the same?

like image 553
w2lame Avatar asked Jul 30 '11 13:07

w2lame


People also ask

How do you enter a user input in a while loop in Java?

Use while Loop With User Input in JavaThe loop will continue to take user inputs till there is an input number that is not a member of the array. Let's try multiple inputs till the loop breaks. The loop will break when a number is entered which is not a member of the array. Run code here.

How do you ask for input in a loop in Python?

First Read the input number asking for the length of the list using the input() function and store these values into a list. After that, you can use the sum() function or Iterate each element in the list using for loop and sump up all.


1 Answers

Read from the controlling terminal device:

read input </dev/tty 

more info: http://compgroups.net/comp.unix.shell/Fixing-stdin-inside-a-redirected-loop

like image 144
dank Avatar answered Oct 05 '22 12:10

dank