linux shell脚本判断文件或文件夹是否存在循环操作

我最近写的脚本 你改改就能用
3.sh中全是 /data/test 这样的指向目录的数据

#!/bin/bash
#set -x

#循环读取文件目录
for line in `cat /root/shizonghao/3.sh`
do
#判断当前文件是否存在
if [ ! -x "$line" ]; then
#如果不存在记录日志
  echo $line>>test.log
fi
done

#shell判断文件夹是否存在

#如果文件夹不存在,创建文件夹
if [ ! -d “/Top” ]; then
mkdir -p /Topfi

#shell判断文件,目录是否存在或者具有权限

folder="/Top"
file="/Top/test.txt"

-x 参数判断 $folder 是否存在并且是否具有可执行权限

if [ ! -x “ f o l d e r " ] ; t h e n m k d i r " folder"]; then mkdir " folder"];thenmkdir"folder”
fi

-d 参数判断 $folder 是否存在

if [ ! -d “ f o l d e r " ] ; t h e n m k d i r " folder"]; then mkdir " folder"];thenmkdir"folder”
fi

-f 参数判断 $file 是否存在

if [ ! -f “ f i l e " ] ; t h e n t o u c h " file" ]; then touch " file"];thentouch"file”
fi

-n 判断一个"变量"是否有值

if [ ! -n “ f i l e " ] ; t h e n e c h o " file" ]; then echo " file"];thenecho"file 变量为空!”
exit 0
fi

判断两个变量的字符串内容是否相同

if [ “ f i l e 1 " = " file1" = " file1"="file2” ]; then
echo “$file1 equal f i l e 2 " e l s e e c h o " file2" else echo " file2"elseecho"file1 not equal $file2”
fi

#!/bin/sh

说明:判断文件是否存在

myPath="/Top"
myFile="/Top/access.log"

这里的-x 参数判断$myPath是否存在并且是否具有可执行权限

if [ ! -x “ m y P a t h " ] ; t h e n m k d i r " myPath"]; then mkdir " myPath"];thenmkdir"myPath”
fi

这里的-d 参数判断$myPath是否存在

if [ ! -d “ m y P a t h " ] ; t h e n m k d i r " myPath"]; then mkdir " myPath"];thenmkdir"myPath”
fi

这里的-f参数判断$myFile是否存在

if [ ! -f “ m y F i l e " ] ; t h e n t o u c h " myFile" ]; then touch " myFile"];thentouch"myFile”
fi

其他参数还有-n,-n是判断一个变量是否是否有值

if [ ! -n “ m y V a r " ] ; t h e n e c h o " myVar" ]; then echo " myVar"];thenecho"myVar 变量为空!”
exit 0
fi

判断$file字符串内容是否是“123123”相同

if [ “ f i l e 1 " = " 123123 " ] ; t h e n e c h o " file1" = "123123" ]; then echo " file1"="123123"];thenecho"file1 equal f i l e 2 " e l s e e c h o " file2" else echo " file2"elseecho"file1 not equal $file2”
fi

猜你喜欢

转载自blog.csdn.net/weixin_44578029/article/details/111229980