0

I have a file parama.out having 3 variables like:

p_id
P_level
p_name

And the value for these variable will be User Input at the run-time. I tried using while loop but instead of asking for the user input its moving to the next line and throwing an error.

I used the code:

cat params.out |\
while read line
do

echo "Please provide the value for $line:\c"
read $line

done
jsosnowski
  • 1,560
  • 3
  • 26
  • 56
  • possible duplicate of [Bash Scripting & Read File line by line](http://stackoverflow.com/questions/10929453/bash-scripting-read-file-line-by-line) – gavv Sep 24 '15 at 11:57

1 Answers1

1

There are a few things I used to make this work.

Put "read $line" on a seperate line (or user && or ; to seperate the two commands).

Remove the "\" after pipe to while (I removed the cat and pipe way to input the file).

Add "/dev/tty" to the read command, to let it know where the input should come from. Otherwise it'll take input from the file.

while read line
do
    echo "Please provide the value for $line:" 
    read $line </dev/tty
    echo "p_id: $p_id"
    echo "p_level: $p_level"
    echo "p_name: $p_name"
done < params.out

This gives the following output:

Please provide the value for p_id:
a
p_id: a
p_level: 
p_name: 
Please provide the value for p_level:
s
p_id: a
p_level: s
p_name: 
Please provide the value for p_name:
d
p_id: a
p_level: s
p_name: d