1.概述
允许测试Linux文件系统上文件的目录和状态。
2.详解
2.1 检查目录
-d测试会检查指定的目录是否存在于系统中。当我们打算将文件写入目录或是准备切换到该目录时,先进行测试是比较好的做法。
#!/bin/bash
#Look before you leap
#
jump_dir=/home/songzhen
#
if [ -d $jump_dir ]
then
echo "The $jump_dir directory exists"
cd $jump_dir
ls
else
echo "The $junp_dir directory does not exist"
fi
执行该脚本,结果如下:
2.2 检查对象是否存在(文件或目录)
-e比较允许你的脚本代码在使用文件或者目录前先检查它们是否存在。
#!/bin/bash
#Check if either a directory or file exists
#
location=$HOME
file_name="sentinel"
#
if [ -e $location ]
then
echo "OK on the $location directory."
echo "Now checking on the file, $file_name."
#
if [ -e $location/$file_name ]
then
echo "OK on the filename"
echo "Updating Current Date..."
date >> $location/$file_name
#
else
echo "File not exist"
echo "touch the File"
touch $file_name
fi
#
else
echo "The $location directory does not exist."
echo "Nothing to update"
fi
执行该脚本,结果如下:
2.3 检查文件
如果确定了对象为文件,必须用-f比较。
#!/bin/bash
#Check if either a directory or file exists
#
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
执行该脚本,结果如下:
2.4 检查是否可读
在尝试从文件中读取数据之前,最好先测试一下文件是否可读,可以使用-r比较测试。
#!/bin/bash
#testing if you can read a file
pwfile=/etc/shadow
#
# first, test if the file exists, and is a file
if [ -f $pwfile ]
then
#now test if you can read it
if [ -r $pwfile ]
then
tail $pwfile
else
echo "Sorry, I am unable to read the $pwfile file"
fi
else
echo "Sorry, the file $pwfile does not exist"
fi
执行该脚本,结果如下:
2.5 检查是否可写
-w比较会判断你对文件是否有可写权限。
#!/bin/bash
#Check if either a directory or file exists
#
item_name=$HOME/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?"
echo
#
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
#
执行该脚本,结果如下:
2.6 检查空文件
用-s比较来检查文件是否为空,尤其是在不想删除非空文件时经常使用,-s比较成功则说明文件中有数据。
#!/bin/bash
#Testing if a file is empty
#
file_name=$HOME/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 "The $file_name does not exist."
fi
执行该脚本,结果如下:
2.7 检查文件是否可以执行
-x比较是判断特定文件是否有执行权限的一个简单办法。
#!/bin/bash
#testing file execution
#
if [ -x test9.sh ]
then
echo "You can run the script:"
else
echo "Sorry,You are unable to execute the script"]
fi
执行该脚本,结果如下:
2.8 检查所属关系
-O比较可以测试出你是否是文件的属主
#!/bin/bash
#check file ownership
#
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
执行该脚本,结果如下:
2.9 检查默认属组关系
-G比较会检查文件的默认组,如果它匹配了用户的默认组,则测试成功,-G比较只会检查默认组而非用户所属的所有组。
#!/bin/bash
#check file group test
#
if [ -G $HOME/testing ]
then
echo "You are in the same group as the file"
else
echo "The file is not owned by your group"
fi
执行该脚本,结果如下:
2.10 检查文件日期
-nt和-ot用来对两个文件的创建日期进行比较,在编写软件安装脚本时非常有用,但是要注意的是要先确认文件是存在的。
#!/bin/bash
#testing file dates
if [ test9.sh -nt test8.sh ]
then
echo "The test9.sh file is newer than test8.sh."
else
echo "The test8.sh file is newer than test9.sh."
fi
执行该脚本,结果如下: