您的位置:首页 > 运维架构 > Shell

shell script中的 if判断,及test命令

2016-04-13 17:18 447 查看
=====================test命令==============================

1.数值比较

if [ n1 -eq n2 ] 检查n1是否等于n2 n1==n2 ?

if [ n1 -ne n2 ] 检查n1是否不等于n2 n1!=n2 ?

if [ n1 -ge n2 ] 检查n1是否大于等于n2 n1>=n2 ?

if [ n1 -gt n2 ] 检查n1是否大于n2 n1>n2 ?

if [ n1 -le n2 ] 检查n1是否小于等于n2 n1<=n2 ?

if [ n1 -lt n2 ] 检查n1是否小于n2 n1<n2 ?

#!/bin/bash
#using number test comparisons
int1=10
int2=11
int3=5
int4=10
if [ $int1 -gt int1 ]
then
echo "$int1 is greater than $int2"
else
echo "$int1 is not greater than $int2"
fi
if [ $int3 -lt $int2 ]
then
echo "$int3 is less than $int2"
else
echo "$int3 is not less than $int2"
fi
if [ $int4 -eq $int1 ]
then
echo "$int4 is equal $int1"
else

echo "$int4 is equal $int1"

fi

注:test命令中只能使用整数,不能比较浮点数

2. 字符串变量表达式 引用变量要带$

= 作为等于时,其两边都必须加空格,否则失效

等号也是操作符,必须和其他变量,关键字,用空格格开 (等号做赋值号时正好相反,两边不能有空格)

if [ str1 = str2 ] 检查str1是否等于str2 字符串允许使用赋值号做等号
if [ str1 != str2 ] 检查str1是否不等于str2

注:转义符号,否则> <会被当成重定向符号 比较方式与sort不同,a<b; A<a
if [ str1 \< str2 ] 检查str1是否小于str2
if [ str1 \> str2 ] 检查str1是否大于str2

if [ -z str1 ] 检查str1是否为空 注:若str1不存在也返回0
if [ -n str1 ] 检查str1是否非空

If [ str ] 等价于 if [ -n str ] 如果字符串变量非空(then) , 空(else)

3.文件表达式

if [ -d file ] 检查file是否存在,并是一个目录

if [ -e file ] 检查file是否存在

if [ -f file ] 检查file是否存在,并是一个文件

if [ -r file ] 检查file是否存在,并可读

if [ -w file ] 检查file是否存在,并可写

if [ -x file ] 检查file是否存在,并可执行

if [ -s file ] 检查file是否存在,并非空

if [ -O file ] 检查file是否存在,并属于当前用户所有

if [ -G file ] 检查file是否存在,并且默认组与当前用户相同

if [ file1 -nt file2 ] 检查file1是否比file2新

if [ file1 -ot file2 ] 检查file1是否比file2旧

===================if其他复合判断=========================

逻辑非 ! 条件表达式的相反

if [ ! 表达式 ]

if [ ! -d $num ] 如果不存在目录$num

逻辑与 –a 条件表达式的并列

if [ 表达式1 –a 表达式2 ]

逻辑或 -o 条件表达式的或

if [ 表达式1 –o 表达式2 ]

逻辑表达式

表达式与前面的= != -d –f –x -ne -eq -lt等合用

逻辑符号就正常的接其他表达式,没有任何括号( ),就是并列

if [ -z "$JHHOME" -a -d $HOME/$num ]

注意逻辑与-a与逻辑或-o很容易和其他字符串或文件的运算符号搞混了

最常见的赋值形式,赋值前对=两边的变量都进行评测

左边测变量是否为空,右边测目录(值)是否存在(值是否有效)

[macg@mac-home ~]$ vi test.sh

echo "input the num:"

read num

echo "input is $num"

if [ -z "$JHHOME" -a -d $HOME/$num ] 如果变量$JHHOME为空,且$HOME/$num目录存在

then

JHHOME=$HOME/$num 则赋值

fi

echo "JHHOME is $JHHOME"

-----------------------

[macg@mac-home ~]$ sh test.sh

input the num:

ppp

input is ppp

JHHOME is

目录-d $HOME/$num 不存在,所以$JHHOME没被then赋值

[macg@mac-home ~]$ mkdir ppp

[macg@mac-home ~]$ sh test.sh

input the num:

ppp

input is ppp

JHHOME is /home/macg/ppp

一个-o的例子,其中却揭示了”=”必须两边留空格的问题

echo "input your choice:"

read ANS

if [ $ANS="Yes" -o $ANS="yes" -o $ANS="y" -o $ANS="Y" ]

then

ANS="y"

else

ANS="n"

fi

echo $ANS

[macg@machome ~]$ sh test.sh

input your choice:

n

y

[macg@machome ~]$ sh test.sh

input your choice:

no

y

为什么输入不是yes,结果仍是y(走then)

因为=被连读了,成了变量$ANS="Yes",而变量又为空,所以走else了

[macg@machome ~]$ vi test.sh

echo "input your choice:"

read ANS echo "input your choice:"

read ANS

if [ $ANS = "Yes" -o $ANS = "yes" -o $ANS = "y" -o $ANS = "Y" ]

then

ANS="y"

else

ANS="n"

fi

echo $ANS

[macg@machome ~]$ sh test.sh

input your choice:

no

n

[macg@machome ~]$ sh test.sh

input your choice:

yes

y

[macg@machome ~]$ sh test.sh

input your choice:

y

y

===================以 test 条件表达式 作为if条件=======================

if test $num -eq 0 等价于 if [ $num –eq 0 ]

test 表达式,没有 [ ]

if test $num -eq 0

then

echo "try again"

else

echo "good"

fi

======================if简化语句=================================

最常用的简化if语句

&& 如果是“前面”,则“后面”

[ -f /var/run/dhcpd.pid ] && rm /var/run/dhcpd.pid 检查 文件是否存在,如果存在就删掉

|| 如果不是“前面”,则后面

[ -f /usr/sbin/dhcpd ] || exit 0 检验文件是否存在,如果存在就退出

用简化 if 和$1,$2,$3来检测参数,不合理就调用help

[ -z "$1" ] && help 如果第一个参数不存在(-z 字符串长度为0 )

[ "$1" = "-h" ] && help 如果第一个参数是-h,就显示help

例子

#!/bin/sh

[ -f "/etc/sysconfig/network-scripts/ifcfg-eth0" ] && rm -f /etc/sysconfig/network-scripts/ifcfg-eth0

cp ifcfg-eth0.bridge /etc/sysconfig/network-scripts/ifcfg-eth0

[ -f "/etc/sysconfig/network-scripts/ifcfg-eth1" ] && rm -f /etc/sysconfig/network-scripts/ifcfg-eth1

cp ifcfg-eth1.bridge /etc/sysconfig/network-scripts/ifcfg-eth1

[ -f "/etc/sysconfig/network-scripts/ifcfg-eth0:1" ] && rm -f /etc/sysconfig/network-scripts/ifcfg-eth0:1

=======================if 的高级特性============================

1.用于数学表达式的双圆括号 (( expression ))

双括号命令符号:

val++ 后增 val-- 后减 ++val 先增 --val 先减

!逻辑求反 ~位求反 **幂运算 <<左移 >>右移

&位与 |位或 &&逻辑与 ||逻辑或

if (( $val1**2 >90 )); then #双括号里的> <不用加转义符号,这是(())的高级特性

(( val2 = $val1 ** 2 ))

echo "The square of $val1 is $val2"

fi

若val1=10,则执行上个脚本的输出为:The square of 10 is 100

2.用于高级字符串处理功能的双方括号[[ expression ]]

在[[ ]]中可以使用正则表达式来实现模式匹配

if [[ $USER == r* ]];then #r*模式匹配

echo "Hello $USER"

else

echo "Sorry,I do not know you"

fi
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: