15-判断大小
原创大约 2 分钟
#!/bin/bash
# 这个脚本演示了如何使用条件语句判断两个数字的大小关系
# 检测是否为数字
checkNum(){
local num=$1
expr $num + 1 &>/dev/null
if [[ $? -ne 0 ]];then
echo "$num 不是一个数字."
exit 1
fi
}
# 提示用户输入第一个数字
echo "Enter the first number:"
read num1
checkNum $num1
# 提示用户输入第二个数字
echo "Enter the second number:"
read num2
checkNum $num2
# 判断大小关系
# -gt是>
if [ "$num1" -gt "$num2" ]; then
echo "第一个数字 $num1 大于 第二个数字 $num2."
# -lt是<
elif [ "$num1" -lt "$num2" ]; then
echo "第一个数字 $num1 小于 第二个数字 $num2."
else
echo "第一个数字 $num1 等于 第二个数字 $num2."
fi