Computing.Net > Forums > Programming > read from user within loop

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

read from user within loop

Reply to Message Icon

Name: sohaib102
Date: April 24, 2009 at 23:38:13 Pacific
OS: ubuntu
Subcategory: General
Comment:

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....



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: April 25, 2009 at 20:03:42 Pacific
Reply:

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 


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: read from user within loop

Reading from a text file in C++ www.computing.net/answers/programming/reading-from-a-text-file-in-c/8983.html

java arrays and reading from file www.computing.net/answers/programming/java-arrays-and-reading-from-file/14305.html

Reading from input www.computing.net/answers/programming/reading-from-input/1305.html