格式如下:
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
双括号命令允许在比较中包含高级数据公式,test命令中只允许在比较中进行简单的算术操作。双圆括号命令允许提供使用更多的数学符号.
#!/bin/bash
val1=10
if (( $val1 ** 2 > 90 ))
then
#**为取幂
(( val2 = $val1 ** 2))
echo "val2 is $val2"
fi
在双圆括号中,不必转义大于号。
格式如下,其提供一个强大的功能:模式匹配,在模式匹配中,可以定义于字符串值相匹配的正则表达式
[[ expression ]]
看一个栗子
#!/bin/bash
if [[ $USER == r* ]]
then
echo "hello $USER"
else
echo "sorry, i don't know you"
fi