Bash Ubuntu脚本永无止境循环
我刚刚开始为大学编写脚本,我正在尝试制作一个菜单,在选中时可以运行一些功能。现在我只是想让菜单出现,但它陷入了一个无限循环。我一点也不擅长编写脚本,但我真的需要在课程中学习它Bash Ubuntu脚本永无止境循环,bash,shell,ubuntu,scripting,Bash,Shell,Ubuntu,Scripting,我刚刚开始为大学编写脚本,我正在尝试制作一个菜单,在选中时可以运行一些功能。现在我只是想让菜单出现,但它陷入了一个无限循环。我一点也不擅长编写脚本,但我真的需要在课程中学习它 while true do echo "1) option1" echo "2) option2" echo "3) option3" done 在条件所在的while循环中,您提供的是true,这意味着条件将始终为true,因此,它是一个无限循环 while循环就是这样工作的: while co
while true
do
echo "1) option1"
echo "2) option2"
echo "3) option3"
done
在条件所在的while循环中,您提供的是true,这意味着条件将始终为true,因此,它是一个无限循环 while循环就是这样工作的:
while condition
if condition is TRUE--> then go inside loop and do operations as per instructions in it.
if condition is FALSE--> then come out of loop since the given condition is no more.
非常基本的while循环示例:
对于shell中的菜单,请使用select语句:
PS3='Select your choice: '
select ans in "option1" "option2" "option3" quit
do
case $ans in
option1) do_something ;;
option2) do_something ;;
option3) do_something ;;
quit) break ;;
esac
done
欢迎来到这里,在条件的while循环中,您提供的是true,这意味着条件将始终为true,因此是一个无限循环。我的讲师添加了while true,这让我感到困惑,因为它没有任何我感到困惑的条件。你会推荐我使用什么作为条件,因为我尝试过的一切都不起作用?@J.walker,我添加了一个while循环的基本示例,你可以搜索论坛本身来获得更多的示例。
Hey there...
Hey there...
Hey there...
Hey there...
PS3='Select your choice: '
select ans in "option1" "option2" "option3" quit
do
case $ans in
option1) do_something ;;
option2) do_something ;;
option3) do_something ;;
quit) break ;;
esac
done