0

I don't know why this happens but I have the following snipped:

exec<$filename
while read line
do
...
done

in order to read a file line by line

after that I have

while true
do
echo "message"
read WISH2
case $WISH2 in 
   y|Y|yes|Yes) dosomething; break ;;
   n|N|no|No) EXIT ;;
   *) echo "Not valid option";
esac
done

what happens is that the last loop never stops at the read! just displays message message message message

does anyone know how to fix this?!

0

3 Answers 3

2

I'm pretty sure you meant exit instead of EXIT. In your code that's the only way it could continuously print "message".

Another problem is that you're not checking for EOF.

Sign up to request clarification or add additional context in comments.

Comments

0

what happens is that the last loop never stops at the read!

Your 2nd loop is expecting read to get input from the console but earlier you redirected STDIN to your file with exec<$filename, don't do that.

Instead use:

while read line
do
...
done < "$filename"

Also, as others have noted you want exit instead of EXIT

Comments

0

Potential Issue in Snippet 1:

exec<$filename # This is redirecting to STDIN. Quotes missing. 
while read line
do
…
done

Potential Solution:

exec 4<"$filename" # Redirected to 4 {1-3 and 9+ are used by Shell} 
while read line
do
…
done <&4

OR

while read line
do
…
done < "$filename"

Potential Issue with snippet 2:

while true
do
echo "message"
read WISH2
case $WISH2 in # missing quotes
   y|Y|yes|Yes) dosomething; break ;; #Space between do
   n|N|no|No) EXIT ;; # exit should be in lower case
   *) echo "Not valid option"; # missing a semi-colon
esac
done

Potential Solution to snippet 2:

while :
do
echo "message"
read WISH2
case "$WISH2" in 
   y|Y|yes|Yes) do something; break ;; 
   n|N|no|No) exit ;; 
   *) echo "Not valid option";; 
esac
done

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.