逻辑连接符
[] test中使用 [[]]中使用 说明
-a && and 两者都为真才为真
-o || or 两者有一个为真即为真
! ! not 相反为真
如果使用[]但又想使用&&可以通过[] && [] 这种形式
练习
输入两个数并比较大小,不能用if
直接输入两个数
#!/bin/bash
#判断参数个数
[ $# -ne 2 ] && {
echo "USAGE: num1 num2"
exit 1
}
#判断整数
[ "`echo "$1"|sed -r 's#[^0-9]##g'`" = "$1" ]||{
echo "first arg must be int."
exit 2
}
[ "`echo "$2"|sed -r 's#[^0-9]##g'`" = "$2" ]||{
echo "seconde arg must be int."
exit 2
}
#比较
[ $1 -lt $2 ]&& {
echo "$1<$2"
exit 0
}
[ $1 -eq $2 ]&& {
echo "$1=$2"
exit 0
}
[ $1 -gt $2 ]&& {
echo "$1>$2"
exit 0
}
read 方式
#!/bin/bash
read -p "pls input 2 num:" num1 num2
a=$num1
b=$num2
#判断参数个数
[ -z "$a" -o -z "$b" ] && {
echo "USAGE: num1 num2"
exit 1
}
#判断整数
[ "`echo "$a"|sed -r 's#[^0-9]##g'`" = "$a" ]||{
echo "first arg must be int."
exit 2
}
[ "`echo "$b"|sed -r 's#[^0-9]##g'`" = "$b" ]||{
echo "seconde arg must be int."
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
}