Shell中的test命令(2)
文件比较
-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新 -nt new
file1 -ot file2 #检查file1是否比file2旧 -ot old
#这些条件使你能在shell脚本中检查文件系统中的文件
并且经常用在要访问文件的脚本中,被广泛来使用
-d file #检查file是否存在并是一个目录
[root@localhost test]# cat 07.sh
#!/bin/bash if [ -d $HOME ] #判断是否存在用户的家目录 then echo "Your HOME directory exits" cd $HOME ls -a else echo "There is a problem with your HOME directory" fi
[root@localhost test]# sh 07.sh
Your HOME directory exits
. .bashrc Documents Music .viminfo
.. .cache Downloads Pictures .xauthY2r7hE
anaconda-ks.cfg .config .esd_auth Public
.bash_history .cshrc .ICEauthority .tcshrc
.bash_logout .dbus initial-setup-ks.cfg Templates
.bash_profile Desktop .local Videos
$HOME :也是个环境变量,系统已经提前给一些变量赋予了值,
$HOME获取当前用户的家目录(环境变量一般是大写的)
$USER:是获取当前系统登陆的用户
[root@localhost ~]# echo $HOME
/root
-e file #检查file是否存在
#!/bin/bash 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, you do not have a HOME directory" fi
[root@localhost test]# sh 08.sh
OK on the directory. now to check the file
Creating new file
[root@localhost test]# cd
[root@localhost ~]# ls
anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates Videos
Desktop Downloads Music Public testing
[root@localhost ~]# cat testing
Wed Feb 12 23:04:26 CST 2020
第二次再执行这个文件时,就是追加了
[root@localhost ~]# cd -
/mnt/test
[root@localhost test]# sh 08.sh
OK on the directory. now to check the file
Appending date to existing file
[root@localhost test]# cd -
/root
[root@localhost ~]# cat testing
Wed Feb 12 23:04:26 CST 2020
Wed Feb 12 23:08:19 CST 2020
[root@localhost ~]#
-f file #检查file是否存在并是一个文件
[root@localhost test]# cat 09.sh
#!/bin/bash if [ -e $HOME ] then echo "The object exists. is it a file ?" if [ -f $HOME ] then echo "Yes. it is not 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
[root@localhost test]# sh 09.sh
The object exists. is it a file ?
No. it is not a file!
But this is a file
[root@localhost ~]# ls -a
. .bashrc Documents Music Videos
.. .cache Downloads Pictures .viminfo
anaconda-ks.cfg .config .esd_auth Public .xauthY2r7hE
.bash_history .cshrc .ICEauthority .tcshrc
-r file #检查file是否存在并可读
[root@localhost test]# cat 10.sh
#!/bin/bash pwdfile=/etc/shadow if [ -f $pwdfile ] then if [ -r $pwdfile ] then tail $pwdfile else echo "Sorry. I am unable to read the $pwdfile file" fi else echo "Sorry. the file $pwdfile dose not exist" fi
[root@localhost test]# sh 10.sh
pulse:!!:18290::::::
gdm:!!:18290::::::
gnome-initial-setup:!!:18290::::::
avahi:!!:18290::::::
postfix:!!:18290::::::
ntp:!!:18290::::::
sshd:!!:18290::::::
tcpdump:!!:18290::::::
-s file #检查file是否存在并非空
[root@localhost test]# cat 11.sh
#!/bin/bash file=test touch $file if [ -s $file ] then echo "The $file file exits and has data in it" else echo "The $file exists and is empty" fi
[root@localhost test]# sh 11.sh
The test exists and is empty
[root@localhost test]# ls
01.sh 03.sh 05.sh 07.sh 09.sh 11.sh
02.sh 04.sh 06.sh 08.sh 10.sh test
[root@localhost test]# date
Thu Feb 13 09:02:58 CST 2020
[root@localhost test]# date > test
[root@localhost test]# vim test
[root@localhost test]#
[root@localhost test]# cat test
Thu Feb 13 09:03:08 CST 2020
[root@localhost test]# sh 11.sh
The test file exits and has data in it
-w file #检查file是否存在并可写
脚本功能 : 测试是否可写,此脚本要在普通用户的权限测试
[root@localhost test]# cp 12.sh /home/student/
#我们要使用student用户执行该脚本,将脚本拷到student的家目录下
[root@localhost test]# cd /home/student/
[root@localhost student]# ls
12.sh
[root@localhost student]# ll
total 4
-rw-r--r-- 1 root root 427 Feb 13 09:15 12.sh
#我们可以看到其他普通用户的权限是r--
[root@localhost test]# cat 12.sh
#!/bin/bash
#测试是否可写
logfile=testroot
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"
chmod u+w $logfile
fi
if [ -w $logfile ]
then
echo "The program ran at: $now" > logfile
echo "The first attempt succeeded"
else
echo "The first attempt failed"
fi
[root@localhost test]#
[root@localhost student]# chmod +x 12.sh
[root@localhost student]# ll 12.sh
-rwxr-xr-x 1 root root 427 Feb 13 09:15 12.sh
[root@localhost student]# su student
[student@localhost ~]$ ls
12.sh
[student@localhost ~]$ sh 12.sh
The first attempt failed
The first attempt succeeded
[student@localhost ~]$ ls
12.sh logfile testroot
脚本解读:
第一次判断时,testroot文件是不可写的,所以返回尝试失败,
紧接着给文件添加了写权限,再次执行上面的语句即可成功,
将当前时间重定向到logfile文件
-x file #检查file是否存在并可执行
[root@localhost test]# cat 13.sh
#!/bin/bash
if [ -x 10.sh ]
then
echo "You can run the script"
./10.sh
else
echo "Sorry. you are unable to execute the script"
fi
[root@localhost test]# ll 10.sh
-rw-r--r-- 1 root root 218 Feb 16 09:53 10.sh
[root@localhost test]# sh 13.sh
Sorry. you are unable to execute the script
-O file #检查file是否存在并且属当前用户所有
[root@localhost test]# cat 14.sh
#!/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
[root@localhost test]# ll /etc/passwd
-rw-r--r-- 1 root root 2235 Feb 12 15:45 /etc/passwd
#我们可以看到这个文件的所属者是root
[root@localhost test]# sh 14.sh
You are the owner of the /etc/passwd file
[root@localhost test]#
-G file #检查file是否存在并且默认组与当前用户相同
[root@localhost test]# cat 15.sh
#!/bin/bash
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
[root@localhost test]# ll $HOME/testing
-rw-r--r-- 1 root root 58 Feb 12 23:08 /root/testing
[root@localhost test]# sh 15.sh
You are in the same group as the file
file1 -nt file2 #检查file1是否比file2新 -nt new
file1 -ot file2 #检查file1是否比file2旧 -ot old
[root@localhost test]# cat 16.sh
#!/bin/bash
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 the 4.sh file"
fi
[root@localhost test]# sh 16.sh
The 2.sh file is newer than 1.sh
The 3.sh file is older than the 4.sh file