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
1

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.