feat: while和util循环

This commit is contained in:
LouisFonda 2024-07-15 14:46:11 +08:00
parent 6a12113be3
commit 9f86a88401
Signed by: yigencong
GPG Key ID: 36FE627068AA2092
3 changed files with 38 additions and 0 deletions

View File

@ -0,0 +1,10 @@
#!/bin/bash
#Program:
# 判断输入如果用户输入yes结束循环否则继续
#History:
# 2024/07/14 LouisFonda<yigencong@yahoo.com>
until [ "${yn}" == "yes" -o "${yn}" == "YES" ]
do
read -p "是否结束运行" yn
done

View File

@ -0,0 +1,11 @@
#!/bin/bash
#Program:
# 判断输入如果用户输入yes就继续执行否则结束运行
#History:
# 2024/07/14 LouisFonda<yigencong@yahoo.com>
yn="yes"
while [ "${yn}" == "yes" -o "${yn}" == "YES" ]
do
read -p "是否继续运行" yn
done

View File

@ -0,0 +1,17 @@
#!/bin/bash
#Program:
# 输入一个值计算从0到这个值的所有和比如输入100那么输出5050
num=0
sum=0
if [ -n "$1" ] && echo "$1" | grep -Eq "^[-+]?[0-9]+$" && [ "$1" -ge 0 ];then
while [ "$num" -le "$1" ]
do
sum=$(($num+$sum))
num=$((num+1))
done
echo "1-${1}求和的结果为:${sum}"
else
echo "输入的数值不正确必须为非空且大与等于0的整数"
fi