浏览量 4968
2013/09/16 02:11
-d file 检查file是否存在并是一个目录
-e 检查file是否存在
-f 检查file是否存在并使一个文件
-r 检查file是否存在并且可读
-s 检查file是否存在并非空
-w 检查file是否存在并可写
-x 检查file是否存在并可执行
-O 检查file是否存在并属于当前用户所有
-G 检查file是否存在并且默认组与当前用户相同
file1 -nt file2 检查file1是否比file2新
file1 -ot file2 检查file1是否比file2旧
检查目录
#!/bin/bash
if [ -d $HOME ]
then
echo " Your HOME directory exists"
cd $HOME
ls -a
else
echo "There is a problem with your HOME directory"
fi
#sh
Your HOME directory exists
. .. 1 .bash_history .bash_logout .bash_profile .bashrc .cshrc .pki snm
[root@Cnyunwei ~]#
检查对象是否存在
#!/bin/bash
if [ -e $HOME ]
then
echo "OK on the directory. now to check the file"
#checking if a file exists
if [ -e $HOME/testing ]
then
#the file exists. append data to it
echo "Appending date to existing file"
date >> $HOME/testing
else
#the file does not exist. create a new file
echo "Creating new file"
date > $HOME/testing
fi
else
echo "sorry .you do not have a home directory"
fi
第一个检查用-e比较来判断用户是否有 $HOME目录。如过有,下一个-e比较会检查并判断testing文件是否存在与$home目录中。如果不存在,shell脚本会用单个大于号(输出重定向符号)来用date命令的输出创建一个新文件。第二次运行这个shell脚本时,他会使用双大于号,这样他就能将date的输出追加到已经存在的文件后面。
'>'表示如果文件不存在 则创建文件,如果文件存在 则清空文件并输出。'>>'表示 如果文件存在 则追加输出到文件后。
检查文件
-e比较适用于文件和目录。要确定指定的对象是个文件,必须用-f比较:
#!/bin/bash
#check if a file
if [ -e $HOME ]
then
echo "The object exists,is it a file?"
if [ -f $HOME ]
then
echo "Yes. it is a file!"
else
echo "No. it is not a file!"
if [ -f $HOME/.bash_history ]
then echo "But this is a file !"
fi
fi
else
echo "Sorry, the object does not exist"
fi
检查是否可读
#!/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 $file does not exist"
fi
/etc/shadow 文件含有系统用户加密后的密码,所以它对系统上的普通用户是不可读的。-r比较判断出我没有这个文件的读权限,所以test 命令失败了,而且bash shell 执行了if-then语句的else部分。
检查空文件
#!/bin/bash
#testing if a file is empty
file=t15test
touch $file
if [ -s $file ]
then
echo " The $file file exists and has data in it"
else
echo "The $file exists and is empty"
fi
date > $file
if [ -s $file ]
then
echo "The $file has data in it"
else
echo "The $file is still empty"
fi
检查是否可写
#!/bin/bash
logfile=$HOME/t16test
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 attept succeeded"
else
echo "The second attempt failed"
fi
检查是否可执行
#!/bin/bash
if [ -x test16 ]
then
echo "You can run the script: "
./test16
else
echo "Sorry, you are unable to execute the script"
fi
检查所属关系
#!/bin/bash
if [ -O /etc/passwd ]
then
echo "You are the owner of the /etc/passwd file"
else
echo "Sory.you are not the owner of the /etc/passwd file"
fi
检查默认属组庴
#!/bi/bash
if [ -G $HOME/testing ]
then
echo "You are in the same group as the file"
ee
echo "The file is not owned by your group"
fi
检查文件日期
#!/bin/bash
if [ ./test19 -nt ./test18]
then
echo "The test 19 file is newer than test18"
else
echo "The test18 file is newer than test19"
fi
如果文件不存在会显示为后一条命令。
上一篇 搜索 下一篇