read from user within loop

April 24, 2009 at 23:38:13
Specs: ubuntu
Hi,
solution in bash script ?
I want to read a variable from user which is inside a while loop and that while loop takes input from a file but every time i read any variable input comes from file....

See More: read from user within loop

Report •


#1
April 25, 2009 at 20:03:42
One method is to associate the data file with a file handle using the exec command. This script associates file handle 6
and then within the loop explicitly reads from standard input, file handle 1:

#!/bin/bash

exec 6<datafile
while read line
do
  echo "$line"
  echo "read input for var"
  read var <&1
done <&6

The bash shell read command supports a -u option which allows reading from another file handle other than standard input:

#!/bin/bash

exec 6<datafile
while read -u 6 line 
do
  echo "$line"
  echo "read input for var"
  read var <&1
done 


Report •
Related Solutions


Ask Question