diff --git a/tutorials/basic/14-until-loop.sh b/tutorials/basic/14-until-loop.sh new file mode 100644 index 0000000..0181431 --- /dev/null +++ b/tutorials/basic/14-until-loop.sh @@ -0,0 +1,10 @@ +#!/bin/bash +#Program: +# 判断输入,如果用户输入yes结束循环,否则继续 +#History: +# 2024/07/14 LouisFonda + +until [ "${yn}" == "yes" -o "${yn}" == "YES" ] +do + read -p "是否结束运行" yn +done \ No newline at end of file diff --git a/tutorials/basic/15-while-loop.sh b/tutorials/basic/15-while-loop.sh new file mode 100644 index 0000000..05b7613 --- /dev/null +++ b/tutorials/basic/15-while-loop.sh @@ -0,0 +1,11 @@ +#!/bin/bash +#Program: +# 判断输入,如果用户输入yes就继续执行,否则结束运行 +#History: +# 2024/07/14 LouisFonda + +yn="yes" +while [ "${yn}" == "yes" -o "${yn}" == "YES" ] +do + read -p "是否继续运行" yn +done \ No newline at end of file diff --git a/tutorials/basic/16-nums-total.sh b/tutorials/basic/16-nums-total.sh new file mode 100644 index 0000000..e15732e --- /dev/null +++ b/tutorials/basic/16-nums-total.sh @@ -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 \ No newline at end of file