shell脚本——判断文件的属性、内容、、

xiaoxiao2021-02-28  76

#!/bin/sh  #文件路径 :/usr/local/1.txt read myfile1 read myfile2 #判断文件1是否存在 if [ -e $myfile1 ]; then    echo '文件存在';     #判断文件1是否为空       if [ -s $myfile1 ]; then           echo '这个文件不为空';          else           echo '这个文件为空';       fi   else    echo '不存在文件'; fi #判断文件2是否存在 if [ -e $myfile2 ]; then    echo '文件存在';     #判断文件2是否为空       if [ -s $myfile2 ]; then           echo '这个文件不为空';          else           echo '这个文件为空';       fi   else    echo '不存在文件'; fi 1、操作符"-e"用于仅判断文件是否存在 2、有三个操作符用于判断对文件的权限 2.1 "-x"判断是否有执行文件的权限 2.2 "-r" 判断是否有读文件的权限 2.3 "-w"判断是否有写文件的权限 3、另外一些文件操作符可以用来判断文件类型 3.1 "-f" 判断为常规文件 3.2 "-L"或者"-h"判断是否为符号链接文件 3.3 "-S" 判断是否为socket 3.4 "-d"判断是否为目录 3.5 其他一些 -b:判断是否为块设备文件,-c:用于判断是否为字符设备文件 4、"-s"判断文件是否为非空,即文件大小大于0,区分与大写S #文件的字节数 、字数、行数 wc  $myfile1 >aa.txt wc  $myfile2 >bb.txt #文件的大小 du -h  --max-depth=2 $myfile1 >>aa.txt du -h  --max-depth=2 $myfile2 >>bb.txt #文件的权限 stat  $myfile1 |grep Access | awk '{print $2}' >>aa.txt stat  $myfile2 |grep Access | awk '{print $2}' >>bb.txt #文件最后修改时间 stat $myfile1 | grep Modify | awk '{split($3,var,".");print var[1]}' >>aa.txt stat $myfile2 | grep Modify | awk '{split($3,var,".");print var[1]}' >>bb.txt #比较文件aa.txt与文件bb.txt,确定文件1.txt与文件2.txt的大小、权限、修改时间 方法一: (diff aa.txt bb.txt) 方法二: CurRow=1 LastRow=`cat aa.txt | wc -l`     while [ $CurRow -le $LastRow ] do       for x in `awk 'NR=='$CurRow' {print $0}' aa.txt`     do         for y in `awk 'NR=='$CurRow' {print $0}' bb.txt`             do         if [ "$x" == "$y" ];then           echo "$x" >>result.txt         fi         done      done      ((CurRow++)) done   在result.txt文件中,保存了两个文件大小,时间的相同性质。
转载请注明原文地址: https://www.6miu.com/read-77346.html

最新回复(0)