在下面的脚本中 - 提示用户确认他们想要继续运行可能错误的脚本 - 当用户Y
根据提示输入时 - 它将突破case
块,只会再次发送回while
循环。
#! /bin/bash
set -e
echo
echo "bad install start"
echo "-----------------------------------------"
while true; do
read -p "this script will probably fail - do you want to run anyway?" yn
case $yn in
[Yy]*)
##### WHAT GOES HERE?? #####
;;
[Nn]*)
exit ;;
*)
echo "answer y or n" ;;
esac
echo "script has broken out of case back into while loop"
done
echo -e "\e[33m Installing bad packagename \e[0m"
apt-get install sdfsdfdfsd
echo "rest of script - will i keep running?"
当n
输入 时,脚本完全按照需要存在。我想知道如何做到这一点,以便在Y
输入脚本时打破这两个case
和while 块,但不会完全退出。我可以为占位符添加一些东西(“这里有什么??”)来做到这一点吗?
答案1
在用户输入“y”的情况下,您可以退出 while 和 case:
break [n]
Exit from within a for, while, until, or select loop. If n is
specified, break n levels. n must be ≥ 1. If n is greater than
the number of enclosing loops, all enclosing loops are exited.
The return value is 0 unless n is not greater than or equal to
1.
就你而言,你想做break 2
.
答案2
@dhag 有一个很好的答案。您还可以使用:
a=0
while [ "$a" -eq 0 ]; do
...
[Nn]*)
a=1;
;;
...
done