feat: while和util循环
This commit is contained in:
parent
6a12113be3
commit
9f86a88401
10
tutorials/basic/14-until-loop.sh
Normal file
10
tutorials/basic/14-until-loop.sh
Normal 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
|
11
tutorials/basic/15-while-loop.sh
Normal file
11
tutorials/basic/15-while-loop.sh
Normal 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
|
17
tutorials/basic/16-nums-total.sh
Normal file
17
tutorials/basic/16-nums-total.sh
Normal 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
|
Loading…
x
Reference in New Issue
Block a user