Kalpna Kashyap
Kalpna Kashyap

Reputation: 11

Reading values for variable in a file using unix

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

Upvotes: 0

Views: 232

Answers (1)

Stig Jakobsen
Stig Jakobsen

Reputation: 11

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

Upvotes: 1

Related Questions