字符串与if语句
字符串与if语句
1.字符串比较
比较符号两端必须有空格
字符串必须用“”引起来
比较公式 | 比较内容 |
---|---|
a == b | 等于判断 |
a != b | a 不等于判断 |
-z "字符串" | 若串长度为0 则真 |
-n"字符串" | 若串长度不为0 则真 |
!-z =-n | 取反嘛 |
[ "name" = "name" ]
[ "name" != "name" ]
[root@web ~]# [ "$USER" = "root" ]
[root@web ~]# echo $USER
root
[root@web ~]# [ "$USER" = "root" ]
[root@web ~]# echo $?
0
[root@web ~]# [ "$USER" = "alex" ]
[root@web ~]# echo $?
1
[root@web ~]# [ "$USER" != "alex" ]
[root@web ~]# echo $?
0
-z -n
[root@web ~]# name=""
[root@web ~]# [ -z $name ]
[root@web ~]# echo $?
0
[root@web ~]# [ ! -z $name ]
[root@web ~]# echo $?
1
[root@web ~]# name="alex"
[root@web ~]# [ -z $name ]
[root@web ~]# echo $?
1
[root@web ~]# [ -n $name ]
[root@web ~]# echo $?
0
案例:
read -p "请输入名字" name1
[ -z $name1 ] && echo "请输入姓名否则不继续执行" && exit
read -p "请输入年龄" age1
echo $name1 $age1
2.正则比对
[[]] 双括号
[root@web01 scripts]# [ "$USER" = "root" ]
[root@web01 scripts]# echo $? 查看上次结果是否为真 真为0
0
[root@web01 scripts]# [[ "$USER" =~ ^r ]] 正则表达 是以r开头的嘛?
[root@web01 scripts]# echo $?
0
[root@web01 scripts]# [[ "$USER" =~ t$ ]] 正则表达 是以t结尾的嘛?
[root@web01 scripts]# echo $?
0
[root@web01 scripts]# [[ ! "$USER" =~ t$ ]] 正则表达 取反
[root@web01 scripts]# echo $?
1
`判读传参的参数是否为整数
方法1
[root@web01 scripts]# age=188
[root@web01 scripts]#
[root@web01 scripts]#
[root@web01 scripts]# [[ "$age" =~ ^[0-9]+$ ]] 开头数字为0-9 并且结尾也是
[root@web01 scripts]# echo $?
0
[root@web01 scripts]# age=188q
[root@web01 scripts]# [[ "$age" =~ ^[0-9]+$ ]] && echo $?
[root@web01 scripts]# [[ "$age" =~ ^[0-9]+$ ]] || echo $?
1
[root@web01 scripts]# age=18.8
[root@web01 scripts]# [[ "$age" =~ ^[0-9]+$ ]]
[root@web01 scripts]# echo $?
1
方法2
expr $1 + 0 >/dev/null 2>&1
[ $? -ne 0 ] && exit
3.多整数对比
&&,||
&&=a and 乘法
||=o or 加法
[root@shell ~]# a=1
[root@shell ~]# b=2
[root@shell ~]# [ $a -eq 2 -a $b -eq 2 ]&&echo 1||echo 0 全对才对
0
[root@shell ~]# [ $a -eq 1 -a $b -eq 2 ]&&echo 1||echo 0
1
[root@shell ~]# [ $a -eq 2 -o $b -eq 2 ]&&echo 1||echo 0 一个对就对
1
[root@shell ~]# [ $a -eq 1 -o $b -eq 2 ]&&echo 1||echo 0
1
用符号的话 就需要双括号
[root@shell ~]# [[ $a -eq 1 && $b -eq 2 ]]&&echo 1||echo 0
1
[root@shell ~]# [[ $a -eq 2 && $b -eq 2 ]]&&echo 1||echo 0
0
[root@shell ~]# [[ $a -eq 2 || $b -eq 2 ]]&&echo 1||echo 0
1
[root@shell ~]# [[ $a -eq 3 || $b -eq 3 ]]&&echo 1||echo 0
0
[root@shell /server/scripts]# [ 10 -eq 10 -a 100 -ne 100 ]
[root@shell /server/scripts]# echo $?
1
[root@shell /server/scripts]# [ 10 -eq 10 -a 100 -eq 100 ]
[root@shell /server/scripts]# echo $?
0
[root@shell /server/scripts]# [ 10 -eq 10 -o 100 -ne 100 ]
[root@shell /server/scripts]# echo $?
0
案例:传入两个数字,比对两个数字的大小
例如: (要求:判断是否传入两个参数 数字加判断是否整数 禁止使用if)
sh count.sh 10 10
10=10
方法一:
[root@shell /server/scripts]# cat count.sh
#!/bin/bash
##############################################################
# File Name: count.sh
# Time: 2019-11-01-09:38:08
# Author: msy
##############################################################
[ $# -ne 2 ] && echo "please input two number" && exit
[[ "$1" =~ ^[0-9]+$ ]]
[ $? -ne 0 ] && echo "警告!请输入整数" && exit
[[ "$2" =~ ^[0-9]+$ ]]
[ $? -ne 0 ] && echo "警告!请输入整数" && exit
[ $1 -lt $2 ] && echo "$1<$2"
[ $1 -eq $2 ] && echo "$1=$2"
[ $1 -gt $2 ] && echo "$1>$2"
[root@shell /server/scripts]# sh count.sh 7 9
7<9
[root@shell /server/scripts]# sh count.sh 7 a
警告!请输入整数
[root@shell /server/scripts]# sh count.sh b a
警告!请输入整数
[root@shell /server/scripts]# sh count.sh 78
please input two number
[root@shell /server/scripts]# sh count.sh 78 33
78>33
方法二:
[root@shell ~]# vim c1.sh
[ $# -ne 2 ]&&{
echo "USAGE: num1 num2"
exit 1
}
#no.2
[ "`echo "$1"|sed -r 's#[^0-9]##g'`" = "$1" ]
[ "`echo "$2"|sed -r 's#[^0-9]##g'`" = "$2" ]
#no.3
[ $1 -lt $2 ]&&{
echo "$1<$2"
exit 0
}
[ $1 -eq $2 ]&&{
echo "$1=$2"
exit 0
}
[ $1 -gt $2 ]&&{
echo "$1>$2"
exit 0
}
[root@shell ~]# sh c1.sh
USAGE: num1 num2
[root@shell ~]# sh c1.sh 5 6
5<6
[root@shell ~]# sh c1.sh 5 5
5=5
[root@shell ~]# sh c1.sh fdds sfs
first arg must be int.
[root@shell ~]# sh c1.sh 1 sfs
second arg must be int.
4.if判断
单分支
if [你有房];then [ -f file ] && echo ok
if [你有房]
then
我就嫁给你
fi
双分支
if [ 你有房 ] [ -f file ] && echo ok || echo error
then
我就嫁给你
else
拜拜
fi
多分支
if [ 你有房 ]
then
我就嫁给你
elif [ 你有钱 ]
then
我也嫁给你
elif [ 你爸是李刚 ]
then
我也嫁给你
elif [ 活好!运维技术好 ]
then
我倒贴也嫁给你
elif [ 你在老男孩学运维 ]
then
我考虑考虑
else
拜拜
fi
if else-if else
if else-if else 语法格式:
if condition1
then
command1
elif condition2
then
command2
else
commandN
fi
案例:例判断两个变量是否相等
a=10
b=20
if [ $a == $b ]
then
echo "a 等于 b"
elif [ $a -gt $b ]
then
echo "a 大于 b"
elif [ $a -lt $b ]
then
echo "a 小于 b"
else
echo "没有符合的条件"
fi
输出结果:
a 小于 b
案例:输入两个数字 是否为整数 使用if方式
#!/bin/bash
read -p "请输入第一个数字:" num1
read -p "请输入第二个数字:" num2
if [ -z $num1 ]
then
echo "您输入的第一个数字为空"&& exit
elif [ -z $num2 ]
then
echo "您输入的第二个数字为空"&& exit
elif [[ "$num1" =~ ^[0-9]+$ && "$num2" =~ ^[0-9]+$ ]]
then
if [ $num1 -lt $num2 ]
then
echo "$num1<$num2"
elif [ $num1 -gt $num2 ]
then
echo "$num1>$num2"
else
echo "$num1=$num2"
fi
else
echo "您输入了错误的值!"&& exit
fi