第11题:开发shell脚本分别实现以脚本传参以及read读入的方式比较2个整数大小

#!/bin/sh

比较2个数的大小

#by zkg 2019-07-31
read -p "please input tow num:" num1 num2
a=$num1
b=$num2

#必须是2个数
[ -z "$a" -o -z "$b" ]&&{
echo "USAGE:NUM1 NUM2"
exit 1
}

#必须是2个参数是整数
[ "echo "$a"|sed -r 's/[^0-9]//g'" = "$a" ]||{
echo "must be int"
exit 2
}
[ "echo "$b"|sed -r 's/[^0-9]//g'" = "$b" ]||{
echo "must be int"
exit 2
}

#比较两个数的大小
[ "$a" -eq "$b" ]&&{
echo "$a=$b"
exit 0
}
[ "$a" -gt "$b" ]&&{
echo "$a>$b"
exit 0
}
[ "$a" -lt "$b" ]&&{
echo "$a<$b"
exit 0
}

上一篇:QC的安装和配置


下一篇:R语言错误的提示(中英文翻译)