如何验证 while 循环并根据条件重复

问题描述 投票:0回答:1
请帮忙!我被困住了。无论我做什么,脚本都无法正确执行。任何见解表示赞赏。

#!/bin/bash Name="" User_Number="0" read -rp "What is your name? " Name read -rp "Enter a number between 1-10. " User_Number while (($User_Number <= 0 && $User_Number >= 11)) do echo "The number you entered is invalid. Please try again." read -rp "Enter a number between 1-10. " User_Number continue echo "Hey ${Name}, the number you entered is: ${User_Number}." done while ((1 >= $User_Number <= 10)) do echo -e "\nHello ${Name}!" echo "The number you entered is: ${User_Number}" break done
如果用户的号码不在 1-10 范围内,我希望提示他们输入新号码。  发生的情况是,当我测试它时,范围之外的任何数字都会被视为在范围内。

我尝试重新排列 while 循环,将一个循环放在另一个循环之前,但无济于事。我尝试过通过一段 true if/then 语句将它们嵌套在一起,但这也不起作用。我在忽略什么?我尝试通过其他帖子寻求帮助。谢谢。 顺便说一句,我是 bash 脚本新手

bash while-loop do-while
1个回答
0
投票
你不需要脚本的结尾。我的脚本也使用

-lt

 表示小于,使用 
-gt
 表示大于。您应该检查
正确的语法以进行此类比较。

#!/bin/bash Name="" User_Number="0" read -rp "What is your name? " Name read -rp "Enter a number between 1-10: " User_Number while [[ $User_Number -lt 1 || $User_Number -gt 10 ]] do echo "The number you entered is invalid. Please try again." read -rp "Enter a number between 1-10: " User_Number done echo "Hey ${Name}, the number you entered is: ${User_Number}."
    
© www.soinside.com 2019 - 2024. All rights reserved.