shell脚本编程<三>:test命令

test命令

格式如下:

if test condition
then
    command
fi

关于test还可以用[]代替,格式如下,注意[]前括号后,后括号前必须有空格

if [ condition ]
then
    command
fi

数值比较

先看如下参数

n1 -eq n2 #检查n1是否等于n2
n1 -ge n2 #检查n1是否大于或等于n2
n1 -le n2 #检查n1是否小于或等于n2
n1 -gt n2 #检查n1是否大于n2
n1 -lt n2 #检查n1是否小于n2
n1 -ne n2 #检查n1是否不等于n2

注意test不能比较浮点数,见下面栗子

#!/bin/bash
val1='echo "scale=4"; 10 /3 | bc'
echo "the val1 is $val1"
if [ $val1 -gt 3 ]
then
    echo "val1 is greater than 3"
fi

运行会保报错

字符串比较

先看下参数

str1 = str2 
str1 != str2 
str1 > str2 
str1 < str2 
-n str1  #检查str1的长度是否大于0
-z str1  #检查str1的长度是否为0

先试一个栗子

#!/bin/bash
testuser=root
if [ $testuser = $USER ]
then
    echo "welcome $testuser"
fi

再看一个比较大小的栗子,注意>号要使用转义符,否则可能被识别为重定向输出

#!/bin/bash
val1=baseball
val2=hockey
if [ $val1 \> $val2 ]
then
    echo "$val1 is greater than $val2"
else
    echo "$val1 is less than $val2"
fi

注意在test命令中,大写字母小于小写字母,这与sort不同,test命令是按照ASCII码来比较的
再看一个栗子,用来评估一个变量是否包含数据,用-n 和 -z 比较方便

#!/bin/bash
val1=testing
val2=''
if [ -n $val1 ]
then
    echo "the string '$val1' is not empty"
fi

if [ -n $val2 ]
then
    echo "the string '$val2' is not empty"
fi

if [ -n $val3 ]
then
    echo "the string '$val3' is not empty"
fi

注意,如果不确定变量的内容,在数值比较或字符串比较中使用它之前,最好使用-n或-z测试下是否为空

文件比较

见下参数

-d file #检查file是否存在且是一个目录
-e file #检查file是否存在,可以是文件或目录
-f file #检查file是否存在且是一个文件
-r file #检查file是否存在且可读
-s file #检查file是否存在且不为空
-w file #检查file是否存在且可写
-x file #检查file是否存在且可执行
-O file #检查file是否存在且被当前用户拥有
-G file #检查file是否存在且默认组是否为当前用户组
file1 -nt file2 #检查file1是否比file2新
file1 -ot file2 #检查file1是否比file2旧
  • 检查目录
#!/bin/bash
if [ -d $HOME ]
then
    echo "your HOME dir exits"
    cd $HOME
    ls -a
else
    echo "your HOME dir not exits"
fi
  • 检查对象是否存在(使用目录或文件时,检查对象是否存在)
#!/bin/bash
if [ -e $HOME ]
then
    echo "OK on the directory, now lets check the file"
    if [ -e $HOME/testing ]
    then
        echo "Appending date to exiting file"
        date >> $HOME/testing
    else
        echo "createing new file"
        date > $HOME/testing
    fi
else
    echo "Sorry, you donnt have a home dir"
fi
  • 是否可读
#!/bin/bash
pwfile=/etc/shadow
if [ -f $pwfile ]
then
    if [ -r $pwfile ]
    then 
        tail $pwfile
    else
        echo "sorry, can not read the $pwfile file"
    fi
else
    echo "sorry, the file $pwfile doesnot exits"
fi
  • 检查空文件(尤其是想要删除空文件时)
#!/bin/bash
file=test15
touch $file
if [ -s $file ]
then
    echo " the $file file exits and has data in it"
else
    echo "the $file exits and is empty"
fi

date > $file

if [ -s $file ]
then
    echo " the $file file exits and has data in it"
else
    echo "the $file exits and is empty"
fi
  • 检查能否向文件中写入数据
#!/bin/bash
logfile=$HOME/test16
touch $logfile
chmod u-w $logfile
now='date + %Y%m%d-%H%M'

if [ -w $logfile ]
then
    echo "the program ran at : $now > logfile"
    echo "the first attempt succeeded"
else
    echo "the first attempt failed"
fi

chmod u+w $logfile

if [ -w $logfile ]
then
    echo "the program ran at : $now > logfile"
    echo "the second attempt succeeded"
else
    echo "the second attempt failed"
fi
  • 检查是否能够运行文件
if [ -x test16 ]
then
    echo "you can run the script"
    ./test16
else
#-x检查你是否有权限运行此脚本
    echo "you cannot run the script"
fi
  • 检查文件日期
    比较的是两个文件的创建时间,编写脚本安装软件时,很方便,有时可能不想安装一个比系统中已安装的文件更旧的文件
#!/bin/bash
file1=test15
file2=test16
if [ $file1 -nt $file2 ]
then 
    echo "the $file1 file is newer than $file2 file"
else
    echo "the $file1 file is not newer than $file2 file"
fi
  • 检查文件所有权
#!/bin/bash
if [ -O /etc/passwd ]
then 
    echo "you're the owner of the /etc/passwd file"
else
    echo "sorry, you're not the owner of the /etc/passwd file"
fi

复合条件检查

if-then语句可以使用布尔逻辑来合并检查条件,如下

[ condition1 ] && [ condition2 ]
[ condition1 ] || [ condition2 ]

下面看一个脚本程序

#!/bin/bash
if [ -d $HOME ] && [ -w $HOME/testing ]
then 
    echo "the file exits and you can write to it"
else
    echo "i can't write to the file"
fi

if-then使用双括号

双括号命令允许在比较中包含高级数据公式,test命令中只允许在比较中进行简单的算术操作。双圆括号命令允许提供使用更多的数学符号.

#!/bin/bash
val1=10
if (( $val1 ** 2 > 90 ))
then
#**为取幂
    (( val2 = $val1 ** 2))
    echo "val2 is $val2"
fi

在双圆括号中,不必转义大于号。

if-then使用双方括号

格式如下,其提供一个强大的功能:模式匹配,在模式匹配中,可以定义于字符串值相匹配的正则表达式

[[ expression ]]

看一个栗子

#!/bin/bash
if [[ $USER == r* ]]
then
    echo "hello $USER"
else
    echo "sorry, i don't know you"
fi
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值