shell_24.Linux文件比较

这一章代码是比较多的,希望大家能够认真去看代码,然后根据代码了解文件比较,我总觉得如果干讲不结合实际代码的话,理解的终究不会很透彻
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 测试会检查指定的目录是否存在于系统中。如果打算将文件写入目录或是准备切换到某
个目录,那么先测试一下总是件好事:

$ cat jump_point.sh 
#!/bin/bash 
# Look before you leap 
# 
jump_directory=/home/Torfa 
# 
if [ -d $jump_directory ] 
then 
     echo "The $jump_directory directory exists." 
     cd $jump_directory 
     ls 
else 
     echo "The $jump_directory directory does NOT exist." 
fi 
$ 
$ ./jump_point.sh 
The /home/Torfa directory does NOT exist. 
$

3.检查对象是否存在
-e 测试允许在使用文件或目录前先检查其是否存在:

$ cat update_file.sh 
#!/bin/bash 
# Check if either a directory or file exists 
# 
location=$HOME 
file_name="sentinel" 
# 
if [ -d $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 file, $file_name." 
        echo "Updating file's contents." 
        date >> $location/$file_name 
 # 
    else 
        echo "File, $location/$file_name, does NOT exist." 
        echo "Nothing to update." 
    fi 
# 
else
    echo "Directory, $location, does NOT exist." 
    echo "Nothing to update." 
fi 
$ 
$ ./update_file.sh 
OK on the /home/christine directory 
Now checking on the file, sentinel... 
File, /home/christine/sentinel, does NOT exist. 
Nothing to update. 
$

4.检查文件

$ cat dir-or-file.sh 
#!/bin/bash 
# Check if object exists and is a directory or a file 
# 
object_name=$HOME 
echo 
echo "The object being checked: $object_name" 
echo 
# 
if [ -e $object_name ] 
then 
    echo "The object, $object_name, does exist," 
 # 
    if [ -f $object_name ] 
    then 
        echo "and $object_name is a file." 
 # 
    else 
        echo "and $object_name is a directory." 
    fi 
# 
else 
    echo "The object, $object_name, does NOT exist." 
fi
$ 
$ ./dir-or-file.sh 
The object being checked: /home/christine 
The object, /home/christine, does exist, 
and /home/christine is a directory. 
$

变量 object_name 略作修改,将目录$HOME 替换成文件$HOME/sentinel,结果可就不一样了:

$ nano dir-or-file.sh
$ 
$ cat dir-or-file.sh
#!/bin/bash 
# Check if object exists and is a directory or a file 
# 
object_name=$HOME/sentinel 
echo 
echo "The object being checked: $object_name" 
echo 
# 
if [ -e $object_name ] 
    then 
        echo "The object, $object_name, does exist," 
 # 
    if [ -f $object_name ] 
    then 
        echo "and $object_name is a file." 
 # 
    else 
        echo "and $object_name is a directory." 
    fi 
# 
else 
    echo "The object, $object_name, does NOT exist." 
fi 
$ 
$ ./dir-or-file.sh 
The object being checked: /home/christine/sentinel 
The object, /home/christine/sentinel, does exist, 
and /home/christine/sentinel is a file. 
$

5.检查是否可读
在尝试从文件中读取数据之前,最好先使用-r 测试检查一下文件是否可读:

$ cat can-I-read-it.sh 
#!/bin/bash 
# Check if you can read a file 
# 
pwfile=/etc/shadow 
echo 
echo "Checking if you can read $pwfile..."  
if [ -f $pwfile ] 
then 
    if [ -r $pwfile ] 
    then 
        echo "Displaying end of file..." 
        tail $pwfile  
    else 
        echo "Sorry, read access to $pwfile is denied." 
    fi 
else 
    echo "Sorry, the $pwfile file does not exist." 
fi 
$ 
$ ./can-I-read-it.sh 
Checking if you can read /etc/shadow... 
Sorry, read access to /etc/shadow is denied. 
$


6.检查空文件
应该用-s 测试检查文件是否为空,尤其是当你不想删除非空文件时。
要当心,如果-s 测试成功,则说明文件中有数据:

$ cat is-it-empty.sh 
#!/bin/bash 
# Check if a file is empty 
# 
file_name=$HOME/sentinel 
echo 
echo "Checking if $file_name file is empty..." 
echo 
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 exits, but is empty." 
        echo "Deleting empty file..." 
        rm $file_name 
    fi 
else 
    echo "The $file_name file does not exist." 
fi 
$ 
$ ls -sh $HOME/sentinel 
4.0K /home/christine/sentinel 
$ 
$ ./is-it-empty.sh 
Checking if /home/christine/sentinel file is empty... 
The /home/christine/sentinel file exists and has data in it. 
Will not remove this file. 
$


7.检查是否可写
-w 测试可以检查是否对文件拥有可写权限。脚本 can-I-write-to-it.sh 只是脚本 can-I-read-it.sh的修改版。
现在,该脚本不再检查是否可以读取 item_name 文件,而是检查是否有权写入该文件:

$ cat can-I-write-to-it.sh 
#!/bin/bash 
# Check if a file is writable 
# 
item_name=$HOME/sentinel 
echo 
echo "Checking if you can write to $item_name..." 
if [ -f $item_name ] 
then
    if [ -w $item_name ] 
    then 
        echo "Writing current time to $item_name" 
        date +%H%M >> $item_name  
    else 
        echo "Sorry, write access to $item_name is denied." 
    fi  
else 
    echo "Sorry, the $item_name does not exist" 
    echo "or is not a file." 
fi 
$ 
$ ls -o $HOME/sentinel 
-rw-rw-r-- 1 christine 32 May 25 17:08 /home/christine/sentinel 
$ 
$ ./can-I-write-to-it.sh 
Checking if you can write to /home/christine/sentinel... 
Writing current time to /home/christine/sentinel 
$


8.检查文件是否可以执行
-x 测试可以方便地判断文件是否有执行权限。
虽然可能大多数命令用不到它,但如果想在shell 脚本中运行大量程序,那就得靠它了:

$ cat can-I-run-it.sh 
#!/bin/bash 
# Check if you can run a file
#
item_name=$HOME/scripts/can-I-write-to-it.sh
echo
echo "Checking if you can run $item_name..."
if [ -x $item_name ]
then
    echo "You can run $item_name."
    echo "Running $item_name..."
    $item_name
else
    echo "Sorry, you cannot run $item_name."
fi
$
$ ./can-I-run-it.sh
Checking if you can run /home/christine/scripts/can-I-write-to-it.sh...
You can run /home/christine/scripts/can-I-write-to-it.sh.
Running /home/christine/scripts/can-I-write-to-it.sh...
[...]
$


9.检查所有权

$ cat do-I-own-it.sh
#!/bin/bash
# Check if you own a file
#
if [ -O /etc/passwd ]
then
    echo "You are the owner of the /etc/passwd file."
else
    echo "Sorry, you are NOT /etc/passwd file's owner."
fi
$
$ whoami 
christine 
$ 
$ ls -o /etc/passwd 
-rw-r--r-- 1 root 2842 Apr 23 15:25 /etc/passwd 
$ 
$ ./do-I-own-it.sh 
Sorry, you are NOT /etc/passwd file's owner'. 
$


10.检查默认属组关系

$ cat check_default_group.sh 
#!/bin/bash 
# Compare file and script user's default groups 
# 
if [ -G $HOME/TestGroupFile ] 
then 
    echo "You are in the same default group as" 
    echo "the $HOME/TestGroupFile file's group." 
# 
else 
    echo "Sorry, your default group and $HOME/TestGroupFile" 
    echo "file's group are different." 
# 
fi 
$ 
$ touch $HOME/TestGroupFile
$ 
$ ls -g $HOME/TestGroupFile
-rw-rw-r-- 1 christine 0 May 29 13:58 /home/christine/TestGroupFile 
$ 
$ ./check_default_group.sh 
You are in the same default group as 
the /home/christine/TestGroupFile file's group'. 
$ 
$ groups
christine adm cdrom sudo dip plugdev lpadmin lxd sambashare 
$ 
$ chgrp adm $HOME/TestGroupFile
$ 
$ ls -g $HOME/TestGroupFile
-rw-rw-r-- 1 adm 0 May 29 13:58 /home/christine/TestGroupFile 
$ 
$ ./check_default_group.sh 
Sorry, your default group and /home/christine/TestGroupFile 
file's group are different'. 
$


11.检查文件日期

$ cat check_file_dates.sh 
#!/bin/bash 
# Compare two file's creation dates/times 
# 
if [ $HOME/Downloads/games.rpm -nt $HOME/software/games.rpm ] 
then 
    echo "The $HOME/Downloads/games.rpm file is newer" 
    echo "than the $HOME/software/games.rpm file." 
else 
    echo "The $HOME/Downloads/games.rpm file is older" 
    echo "than the $HOME/software/games.rpm file." 
fi 
$ 
$ ./check_file_dates.sh 
The /home/christine/Downloads/games.rpm file is newer 
than the /home/christine/software/games.rpm file. 
$

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微辣已是极限

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值