实现以定义变量,脚本传参以及read读入的方式比较2个整数
read方式:
#!/bin/bash read -p "pls input two num:" a b #测试字符串是否为0 [ -z "$a" ] || [ -z "$b" ] &&{ #-z:检测字符串长度是否为0,为0返回 true。 echo "pls input two num again:" exit 1 } expr $a + 0 &>/dev/null RETVAL1=$? expr $b + 0 &>/dev/null RETVAL2=$? test $RETVAL1 -eq 0 -a $RETVAL2 -eq 0||{ #-a:与运算,两个表达式都为 true 才返回 true。 echo "Pls input two num again: " exit 2 } [ $a -lt $b ] && { echo "$a < $b" exit 0 } [ $a -eq $b ] && { echo "$a = $b" exit 0 } [ $a -gt $b ] && { echo "$a > $b" exit 0 } 验证奇迹的时刻: [[email protected] ~]# sh test1.sh Pls input two num:1 2 1 < 2 [[email protected] ~]# sh test1.sh Pls input two num:1 1 1 = 1 [[email protected] ~]# sh test1.sh Pls input two num:3 1 3 > 1
脚本传参:
#!/bin/bash a=$1 b=$2 [ -z "$a" ] || [ -z "$b" ] && { echo "pls input two num again:" exit 1 } expr $a + 0 &>/dev/null RETVAL1=$? expr $b + 0 &>/dev/null RETVAL2=$? test $RETVAL1 -eq 0 -a $RETVAL2 -eq 0 || { echo "pls input two num again:" exit 2 } [ $a -lt $b ] && { echo "$a < $b" exit 0 } [ $a -eq $b ] && { echo "$a = $b" exit 0 } [ $a -gt $b ] && { echo "$a > $b" exit 0 } 验证奇迹的时刻: [[email protected] ~]# sh test1.sh 2 3 2 < 3 [[email protected] ~]# sh test1.sh 1 1 1 = 1
定义变量:
#!/bin/bash a=1 b=1 [ -z "$a" ] || [ -z "$b" ] && { echo "pls input two num again:" exit 1 } expr $a + 0 &>/dev/null RETVAL1=$? expr $b + 0 &>/dev/null RETVAL2=$? test $RETVAL1 -eq 0 -a $RETVAL2 -eq 0 || { echo "pls input two num again:" exit 2 } [ $a -lt $b ] && { echo "$a < $b" exit 0 } [ $a -eq $b ] && { echo "$a = $b" exit 0 } [ $a -gt $b ] && { echo "$a > $b" exit 0 } 验证奇迹的时刻: [[email protected] ~]# sh test1.sh 1 = 1
时间: 2024-11-05 12:20:17