【shell实战之】测试linux文件系统中文件和目录的状态

测试linux文件系统中文件和目录的状态

#!/bin/bash

#允许测试linux文件系统中文件和目录的状态

#    -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旧
#    注意:如果-nt -ot 比较的文件不存在,则会返回错误的信息,这两个指令并不会检查文件是否存在,所以在使用之前需先确认文件是否存在。

echo "************** 1.test -d ****************"
#location=/home/Torfa
location=/home/z01111/bash_pro

#-d 测试会检查指定的目录是否存在于系统中
if [ -d $location ]
then
    echo "The $location directory exists."
    cd $location
    ls
else
    echo "The $location directory does NOT exists."
fi

#-e 测试允许在使用文件或目录前先检查其是否存在
echo "************** 2.test -e ****************"
file_name="test.sh"
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容易忘记
    fi
else
    echo "Directory, $location, does NOT exist."
    echo "Nothing to update"
fi

#-f 检查指定对象是否为文件
echo "************** 3.test -f ****************"
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 $location/$file_name directory or file exist."
        if [ -f $location/$file_name ]
        then
            echo "OK,file $location/$file exist,on the file,$file_name"
            echo "Updating file's contents."
            date >> $location/$file_name
        else
            echo "NO,file $location/$file does NOT exist"
        fi
    else
        echo "NO,file or directory, $location/$file_name, does NOT exist."
        echo "Nothing to update."
#这里的fi容易忘记
    fi
else
    echo "Directory, $location, does NOT exist."
    echo "Nothing to update"
fi

运行结果:
************** 1.test -d ****************
The /home/z01111/bash_pro directory exists.
bash_basic.sh p244_if-then-elif-else系统用户信息 p267_case_command p284_while命令 test.sh
p223_提取日期信息生成日志文件名 p253_test和条件测试空变量 p270_packageMgrCheck p286_until命令
p233_内联输入重定向与bc联用 p254_file_compare p274_for命令 p288_嵌套循环
p237_计算两日期间隔天数 p263_if-then的高级特性 p282_C语言风格的for命令 p290_循环处理文件数据
************** 2.test -e ****************
OK on the /home/z01111/bash_pro directory
NOW checking on the file,test.sh…
OK on the file,test.sh
Updating file’s contents.
************** 3.test -f ****************
OK on the /home/z01111/bash_pro directory
NOW checking on the file,test.sh…
OK /home/z01111/bash_pro/test.sh directory or file exist.
OK,file /home/z01111/bash_pro/ exist,on the file,test.sh
Updating file’s contents.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值