gpt4 book ai didi

linux - 在无效的输出条件上重复一个特定的 case 语句并在 case 内循环

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:17:27 27 4
gpt4 key购买 nike

我有一个 case 语句,如 1) 2) 3) ... 如下,其中 1) 格式 case 有多个 if else 条件。我想在无效条件下重复特定情况。当用户首先选择 1 时,它会询问果汁的名称。接下来它询问可用性。如果是,它会继续,否则会要求计数并执行操作。如果操作“exit 0”它只是打印完成。如果操作“退出 1”,我希望循环从顶部继续,即从“果汁的名称?”

1)
echo "Name of the Juice ?"
read juice
echo "Is it available? [Y/N]"
read deci
if [ "$deci" = "N" ]
then
echo "Continue.!"
else
echo "how many left in the warehouse ?"
read left
I have a command here which exits 0 or 1
if [ $? -eq 0 ]; then
echo "done"
else
<I have to continue from the start [ie;name of the juice]>
fi
fi
;;
echo "Name of the Pizza ?"
read juice
echo "Is it available? [Y/N]"
read deci
if [ "$deci" = "N" ]
then
echo "Continue.!"
else
echo "how many left ?"
read left
I have a command here which exits 0 or 1
if [ $? -eq 0 ]; then
echo "done"
else
<I have to continue from the start [ie;name of the juice]>
fi
fi
;;

最佳答案

您的代码有点难以阅读,但如果我理解正确的话,您想要在您的一个案例中循环并决定是继续返回到该案例语句的开头,还是离开。这可以通过使用 while : 将特定情况的内容放入无限循环中,然后使用 continue 在任何时候返回到循环的开头来完成,或者使用 break 在任何时候跳出循环。

不传递您的代码的其余部分,但您可以处理 case 1) 的 case 语句类似于:

1)
while :
do
printf "Name of the Juice?: "
read -r juice
printf "Is it available? [Y/N]: "
read -r deci
if [ "$deci" = "N" ] || [ "$deci" = "n" ]
then
printf "Continue.!\n"
else
printf "how many left in the warehouse?: "
read -r left
# I have a command here which exits 0 or 1
if [ $? -eq 0 ]; then
printf "done\n"
break ## break out of loop
else
continue
fi
fi

printf "Name of the Pizza?: "
read -r juice ## why juice - I thought this was pizza?
printf "Is it available? [Y/N]: "
read -r deci
if [ "$deci" = "N" ] || [ "$deci" = "n" ]
then
printf "Continue.!\n"
else
printf "how many left?: "
read -r left
# I have a command here which exits 0 or 1
if [ $? -eq 0 ]; then
printf "done"
break ## break out of loop
else
continue
fi
fi
done
;; # your case termination from where?

请添加对您的任何其他要求的进一步说明,并将您正在处理的完整脚本发布为 A Minimal, Complete, and Verifiable example我很乐意进一步提供帮助。

关于linux - 在无效的输出条件上重复一个特定的 case 语句并在 case 内循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48986193/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com