-1

I am trying to do a while loop while waiting for user input.

read x
while [ $x == 3 ]
do
echo yay
done

does not do what I want. Of course it does not work, but I type that and make sure no one gets confused

tripleee
  • 175,061
  • 34
  • 275
  • 318
belloworld
  • 21
  • 2
  • It's not clear what you were hoping that this code should do. You can run the `while` loop in the background if you start it with `&` before the `read`, but of course a separate background process has no way to know which value the user inputs in the parent process. – tripleee Apr 19 '22 at 10:13
  • See my answer below. Similar to your idea. But I figured it out myself at last. Thanks for your help anyway. – belloworld Apr 19 '22 at 10:31

1 Answers1

2

Well, I figured it out.

loop(){ while true; do echo -n .; sleep 2; done; }
loop & read x
kill %1

This defines a function called loop, runs it in background, waits for user input, then stops the background process.

belloworld
  • 21
  • 2