==:表示测试两个字符串是否相等,相等为真,否则为假
!=:表示测试两字符串是否不等
>:字符串比较大小
<:字符串比较大小
单目运算
-n string:测试字符串是否为空,空为真,否则为假
-s string:测试指定字符串是否不为空,空为假,否则为真
实例
1、写一脚本,通过参数传递用户名,判断用户名是否和基本组名一致,如一致显示ok,否则not same;
[[email protected] ~]# cat test14.sh #!/bin/bash # if ! id $1 &>/dev/null;then echo "No such user." exit 12 fi if [ $1 == `id -n -g $1` ];then echo "Ok" else echo "Not same." fi
2、写一个脚本,通过参数传递,判断输入的如是q,Q,quit,Quit或QUIT退出脚本,否则显示参数
[[email protected] ~]# cat test15.sh #!/bin/bash # if [ $# -lt 1 ];then echo "Argument is less than 1." exit 1 fi # if [ $1 = ‘q‘ ];then echo "Quiting...." exit 2 elif [ $1 = ‘Q‘ ];then echo "Quiting...." exit 3 elif [ $1 = ‘quit‘ ];then echo "Quiting...." exit 4 elif [ $1 = ‘Quit‘ ];then echo "Quiting...." exit 5 elif [ $1 = ‘QUIT‘ ];then echo "Quiting...." exit 6 else echo $1 fi
3、bc命令的学习
1)方法1
[[email protected] ~]# echo "scale=2;33/12;" |bc 2.75
2)方法2
[[email protected] ~]# bc <<<"scale=2;33/12;" 2.75
4、传递三个参数,第一个为整数,第二个为算术运算符,第三个为整数,将计算结果显示出来,并保留两位精度
[[email protected] ~]# cat test16.sh #!/bin/bash # if [ $# -lt 3 ];then echo "Argument:$0 ARG1 ARG2 ARG3" exit 2 fi echo "scale=2;$1$2$3;" | bc
时间: 2024-10-10 09:53:50