开发者

can anyone show me a very simple example of unix script loop

开发者 https://www.devze.com 2023-02-19 11:11 出处:网络
I just need an example of script that repeat all the same actions in a loop til we ask to stop it. Say I want the user to type y or n for exit, how would i implement it. I have something like

I just need an example of script that repeat all the same actions in a loop til we ask to stop it. Say I want the user to type y or n for exit, how would i implement it. I have something like echo "Input y or n to exit" read input if [ "$input = y ] then ....... else ........ fi

For the same script demonstrated in the answer below or maybe other开发者_如何转开发 example, how can I have this addition to make the user control the script without having to exit only by pressing control+z


while true; do echo hello; sleep 1; done

will run until you send a signal.


while true; do
   commands ...

   read -p "Continue (y/n) ? " answer
   case "$answer" in
      Y*|y*) : ;;
      *) break
   esac
done

If the user responds with "Y" or "y", do nothing, in which case the loop continues. Otherwise break the loop.

0

精彩评论

暂无评论...
验证码 换一张
取 消