while和until循环练习
程序员文章站
2022-05-12 10:41:13
...
1.用二个以上的数字为参数,显示其中的最大值和最小值
#!/bin/bash
if [[ $# == 0 ]];then
echo "no arguments"
exit
fi
declare -i biggest=$1
declare -i smallest=$1
until [[ -z $1 ]];do
if (( $1 > $biggest ));then
biggest=$1
fi
if (( $1 < $smallest ));then
smallest=$1
fi
shift
done
echo "the biggest integer is $biggest."
echo "the smallest integer is $smallest."
unset biggest
unset smallest
2.随机生成10以内的数字,实现猜字游戏,提示比较大或小,相等则退出
#!/bin/bash
let rnd=$RANDOM%9+1
while true;do
read -p "guess a integer less than ten:" input
if (( $input < $rnd ));then
echo "your input is too small."
elif (( $input > $rnd ));then
echo "your input is too big."
else
echo "right input."
break
fi
done
3.用文件名做为参数,统计所有参数文件的总行数
#!/bin/bash
declare -i sum=0
until [[ -z $1 ]];do
if [[ -f $1 ]];then
declare -i rows=$(cat $1 | wc -l)
let sum+=$rows
shift
else
echo "not a file."
break
fi
done
echo "the sum of all files's rows is $sum."
上一篇: mysql数据库练习