实现以定义变量,脚本传参以及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 } 验证奇迹的时刻: [root@XCN ~]# sh test1.sh Pls input two num:1 2 1 < 2 [root@XCN ~]# sh test1.sh Pls input two num:1 1 1 = 1 [root@XCN ~]# 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 } 验证奇迹的时刻: [root@XCN ~]# sh test1.sh 2 3 2 < 3 [root@XCN ~]# 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 } 验证奇迹的时刻: [root@XCN ~]# sh test1.sh 1 = 1
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。