shell中的test命令—文件比较
1. 文件比较功能
test命令的文件比较功能:
比较 | 说明 |
---|---|
-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旧 |
2. -d检查file是否存在并是一个目录
#!/bin/bash
#检查file是否存在并是一个目录
if [ -d $HOME ]
then
echo "Your HOME directroy exists"
cd $HOME
ls -a
else
echo "There is a problem with your HOME directory"
fi
注意:此处的 $HOME 是shell中的环境变量
3. -e检查file是否存在
#!/bin/bash
#-e检查file是否存在
if [ -e $HOME ]
then
echo "OK on the directory.now to check the file"
if [ -e $HOME/testing ] #如果文件存在
then
echo "Appending date to existing file"
date >> $HOME/testing #追加date信息到文件中
else #如果文件不存在
echo "Creating new file"
date > $HOME/testing #将date信息输入到文件
fi
else
echo "Sorry,your do not have a HOME directory"
fi
4. -f检查file是否存在并是一个文件
#!/bin/bash
#-f检查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
5. -r检查文件是否存在并可读
#!/bin/bash
# -r检查文件是否存在并可读
pwdfile=/etc/shadow
if [ -f $pwdfile ] #先检查$pwdfile是否存在并是一个文件
then
if [ -r $pwdfile ] #满足上面条件再检查它是否可读
then
tail $pwdfile
else
echo "Sorry,Permission denied.unable to read"
fi
else
echo "Sorry.the file $pwdfile does not exist"
fi
可读的结果:
6. -s检查文件是否存在并为非空
#!/bin/bash
# -s检查文件是否存在并为非空
file=test
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
第一次执行:
第二次执行:
7. -w检查文件是否存在并可写
#!/bin/bash
# -w检查文件是否存在并可写
logfile=logfile
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 first attempt succeeded"
else
echo "The first attempt failed"
fi
执行时使用普通用户:
执行结果:
8. -x检查文件是否存在并可执行
#!/bin/bash
# -x检查文件是否存在并可执行
if [ -x 10.sh ]
then
echo "You can run the script:"
./10.sh
else
echo "Sorry,you are unable to execute the script"
fi
9. -O检查file是否存在并属当前用户所有
#!/bin/bash
# -O检查file是否存在并属当前用户所有
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
10. -G检查文件是否存在并且默认组与当前用户相同
#!/bin/bash
# -G检查文件是否存在并且默认组与当前用户相同
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
11. 比较文件的新旧
#!/bin/bash
# -nt检查file1是否比file2新
# -ot检查file1是否比file2旧
if [ 01.sh -nt 02.sh ]
then
echo "The 1.sh file is newer than 2.sh"
else
echo "The 2.sh file is newer than 1.sh"
fi
if [ 03.sh -ot 04.sh ]
then
echo "The 3.sh file is older than 4.sh"
else
echo "The 4.sh file is older than 3.sh"
fi
12. 多个条件同时比较
#!/bin/bash
if [ -d $HOME ] && [ -w $HOME/testing ]
then
echo "The file exists and you can write it"
else
echo "You cannot write the file"
fi
13. test命令中可使用变量
在test中使用变量需要用双方括号包围[[ ]]
#!/bin/bash
if [[ $USER == r* ]]
then
echo "Hello $USER"
else
echo "Sorry.I do not know you"
fi