- 条件测试还允许比较字符串的值。测试两个字符串之间的比较语法如下:
比较 描述
str1 = str2 检查两者是否相同
str1 != str2 检查两者是否不同
str1 < str2 检查str1是否小于str2
str1 > str2 检查str1是否大于str2
-n str1 检查str1的长度是否非0
-z str1 检查str1的长度是否为0
-
比较字符串的相等性时,比较测试会将所有的标点和大小写情况都考虑在内。字符串的相等性案例如下:
#!/bin/bash
testuser=njust
if [ $USER = $testuser ]
then
echo welcome $testuser
fi
# 结果
[njust@njust tutorials]$ ./str1.sh
welcome njust
- 字符串的不等条件判断案例如下:
#!/bin/bash
testuser=baduser
if [ $USER != $testuser ]
then
echo This is not $testuser
else
echo welcome $testuser
fi
# 结果
[njust@njust tutorials]$ ./str1.sh
This is not baduser
-
字符串顺序:要测试一个字符串是否比另一个字符串大或小时,经常要考虑下面两个问题:
- 大于号或小于号必须转义,否则shell会把它们当作重定向符号,把字符串值当作文件名;
- 大于和小于顺序和sort命令所采用的不同;
- 字符串比较是常出现的错误案例及解决方法:
#!/bin/bash
value1=baseball
value2=football
if [ $value1 > $value2 ]
then
echo $value1 is greater than $value2.
else
echo $value1 is less than $value2.
fi
# 结果
[njust@njust tutorials]$ ./str2.sh
baseball is greater than football.
# 解决方法:对大于号进行转义
#!/bin/bash
value1=baseball
value2=football
if [ $value1 \> $value2 ]
then
echo $value1 is greater than $value2.
else
echo $value1 is less than $value2.
fi
# 结果
[njust@njust tutorials]$ ./str2.sh
baseball is less than football.
- 原因分析:脚本中的大于号被解释成了输出重定向,因此创建了一个名为football的文件。由于重定向的顺利执行,test命令返回了退出状态码0,if语句误以为所有命令都已经执行完成了。
[njust@njust tutorials]$ ls -l football
-rw-rw-r--. 1 njust njust 0 3月 17 14:33 football
- 第二个问题一般出现在经常处理大小写的场合。sort命令处理大写字母的方法刚好和test命令相反。具体原因:比较测试中使用的是标准的ASCII顺序,根据每个字符的ASCII数值来决定排序结果。sort命令使用的是系统的本地化语言设置中定义的排序顺序。具体案例如下:
#!/bin/bash
value1=Curry
value2=curry
if [ $value1 \> $value2 ]
then
echo $value1 is greater than $value2.
else
echo $value1 is less than $value2.
fi
# 结果
[njust@njust tutorials]$ ./str2.sh
Curry is less than curry.
######## sort命令 #########
[njust@njust tutorials]$ cat testfile
Curry
curry
# 结果
[njust@njust tutorials]$ sort testfile
curry
Curry
-
字符串大小:-n和-z可以检查一个变量中是否为空。具体案例如下:
#!/bin/bash
val1=testing
val2=""
if [ -n $val1 ]
then
echo "The string '$val1' is not empty."
else
echo "The string '$val1' is empty."
fi
if [ -z $val2 ]
then
echo "The string '$val2' is empty."
else
echo "The string '$val2' is not empty."
fi
if [ -z $val3 ]
then
echo "The string '$val3' is empty."
else
echo "The string '$val3' is not empty."
fi
# 结果
[njust@njust tutorials]$ ./str3.sh
The string 'testing' is not empty.
The string '' is empty.
The string '' is empty.
- 文件比较可能是shell编程中最强大、用的最多的比较形式。它允许你测试Linux文件系统上文件和目录的状态。文件比较功能语法如下:
比较 描述
-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旧
-
检查目录:-d测试会检查指定的目录是否存在于系统中。一般用于将文件写入目录或准备切换到某个目录中。
#!/bin/bash
dst_dir=/home/curry
if [ -d $dst_dir ]
then
echo "The $dst_dir directory exists"
cd $dst_dir
ls
else
echo "The $dst_dir directory does not exists"
fi
# 结果
[njust@njust tutorials]$ ./file1.sh
The /home/curry directory does not exists
-
检查对象是否存在:-e允许你的脚本代码在使用文件或目录前先检查它们是否存在。
#!/bin/bash
location=$HOME
file_name="sentinel"
mid_path="tutorials"
if [ -e $location ]
then
echo "OK on the $location directory."
echo "Now checking on the file, $file_name."
if [ -e $location/$mid_path/$file_name ]
then
echo "OK on the filename"
echo "Updating Current Date..."
date >> $location/$mid_path/$file_name
else
echo "File does not exist"
echo "Nothing to update"
fi
else
echo "The $location directory does not exist."
echo "Nothing to update"
fi
# 结果
[njust@njust tutorials]$ ./file2.sh
OK on the /home/njust directory.
Now checking on the file, sentinel.
File does not exist
Nothing to update
[njust@njust tutorials]$ ./file2.sh
OK on the /home/njust directory.
Now checking on the file, sentinel.
OK on the filename
Updating Current Date...
-
检查文件:-e可用于文件和目录。要确定指定对象为文件,必须用-f。实例如下:
#!/bin/bash
item_name=$HOME
echo
echo "The item being checked: $item_name"
echo
if [ -e $item_name ]
then
echo "The item, $item_name, does exist."
echo "But is it a file?"
echo
if [ -f $item_name ]
then
echo "Yes, $item_name is a file."
else
echo "No, $item_name is not a file."
fi
else
echo "The item, $item_name, does not exist."
echo "Nothing to update"
fi
# 结果
[njust@njust tutorials]$ ./file3.sh
The item being checked: /home/njust
The item, /home/njust, does exist.
But is it a file?
No, /home/njust is not a file.
- 对上述程序进行小修改,可以观察出结果的不同:
#!/bin/bash
item_name=$HOME/tutorials/sentinel # 注意此句!!!
echo
echo "The item being checked: $item_name"
echo
if [ -e $item_name ]
then
echo "The item, $item_name, does exist."
echo "But is it a file?"
echo
if [ -f $item_name ]
then
echo "Yes, $item_name is a file."
else
echo "No, $item_name is not a file."
fi
else
echo "The item, $item_name, does not exist."
echo "Nothing to update"
fi
# 结果
[njust@njust tutorials]$ ./file3.sh
The item being checked: /home/njust/tutorials/sentinel
The item, /home/njust/tutorials/sentinel, does exist.
But is it a file?
Yes, /home/njust/tutorials/sentinel is a file.
-
检查是否可读:在尝试从文件中读取数据之前,最好先测试一下文件是否可读。可以使用-r比较测试。实例如下:
#!/bin/bash
pwfile=/etc/shadow
if [ -f $pwfile ]
then
if [ -r $pwfile ]
then
tail $pwfile
else
echo "Sorry,I am unable to read the $pwfile file."
fi
else
"Sorry, the file $pwfile does not exist"
fi
# 结果
njust@njust tutorials]$ ./file4.sh
Sorry,I am unable to read the /etc/shadow file.
-
检查空文件:用-s比较来检查文件是否为空,尤其是在不想删除非空文件的时候。注意:当-s比较成功时,说明文件中有数据。实例如下所示:
#!/bin/bash
file_name=$HOME/tutorials/sentinel
if [ -f $file_name ]
then
if [ -s $file_name ]
then
echo "The $file_name file exists and has data in it."
echo "Will not remove this file."
else
echo "The $file_name file exists, but is empty."
echo "Deleting empty file."
rm $file_name
fi
else
echo "File,$file_name, does not exist."
fi
# 结果
[njust@njust tutorials]$ ./file5.sh
The /home/njust/tutorials/sentinel file exists and has data in it.
Will not remove this file.
-
检查文件是否可写:-w比较会判断你对文件是否有写的权限。具体实例如下所示:
#!/bin/bash
item_name=$HOME/tutorials/sentinel
echo
echo "The item being checked: $item_name."
echo
if [ -e $item_name ]
then
echo "The item, $item_name, does exist."
echo "But is it a file?"
echo
if [ -f $item_name ]
then
echo "Yes, $item_name is a file."
echo "But is it writable?"
if [ -w $item_name ]
then
echo "Writing current time to $item_name."
date +%H%M >> $item_name
else
echo "Unable to write to $item_name."
fi
else
echo "No, $item_name is not a file."
fi
else
echo "The item,$item_name, does not exist."
echo "Nothing to update."
fi
# 结果
[njust@njust tutorials]$ ./file6.sh
The item being checked: /home/njust/tutorials/sentinel.
The item, /home/njust/tutorials/sentinel, does exist.
But is it a file?
Yes, /home/njust/tutorials/sentinel is a file.
But is it writable?
Writing current time to /home/njust/tutorials/sentinel.
-
检查文件是否可执行:-x比较是判断特定文件是否有执行权限的一个简单方法。如果你需要在shell脚本中运行大量脚本,它就能发挥作用。
#!/bin/bash
if [ -x file5.sh ]
then
echo "You can run the script: "
./file5.sh
else
echo "Sorry, you are unable to execute the script"
fi
# 结果
[njust@njust tutorials]$ ./file7.sh
You can run the script:
The /home/njust/tutorials/sentinel file exists and has data in it.
Will not remove this file.
-
检查所属关系:-O可以测试你是否是文件的拥有者。实例如下所示:
#!/bin/bash
if [ -O /etc/passwd ]
then
echo "You are the owner of the /etc/passwd file."
else
echo "Sorry,you are not the owner of the /etc/passwd file."
fi
# 结果
[njust@njust tutorials]$ ./file8.sh
Sorry,you are not the owner of the /etc/passwd file.
-
检查默认属组关系:-G比较会检查文件的默认组。如果它匹配了用户的默认组,则测试成功。-G比较只会检查默认组而非用户所属的所有组。
#!/bin/bash
if [ -G $HOME/tutorials/sentinel ]
then
echo "You are in the same group as the file."
else
echo "The file is not owned by your group."
fi
# 结果
[njust@njust tutorials]$ ./file9.sh
You are in the same group as the file.
-
检查文件日期:主要用于对两个文件的创建日期进行比较。通常用于编写软件安装脚本时非常有用。-nt比较会判定一个文件是否比另一个文件新;如果文件较新,则它的文件创建日期更近。-ot比较会判断一个文件是否比另一文件旧。如果文件较旧,意味着它的创建日期更早。
#!/bin/bash
if [ file9.sh -nt file1.sh ]
then
echo "The file9 file is newer than file1."
else
echo "The file1 file is newer than file9."
fi
if [ file7.sh -ot file9.sh ]
then
echo "The file7 file is older than the file9 file."
fi
# 结果
[njust@njust tutorials]$ ./file10.sh
The file9 file is newer than file1.
The file7 file is older than the file9 file.
- 注意:使用-nt或-ot比较文件之前,必须先确认文件是存在的。因为-nt或-ot都不会先检查文件是否存在!
#!/bin/bash
if [ file9.sh -nt file1.sh ]
then
echo "The file9 file is newer than file1."
else
echo "The file1 file is newer than file9."
fi
if [ file7.sh -ot file9.sh ]
then
echo "The file7 file is older than the file9 file."
fi
# 错误的结果,因为badfile1和badfile2文件根本就不存在!
[njust@njust tutorials]$ ./error.sh
The badfile2 file is newer than badfile1.
网友评论