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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

石宗昊

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

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

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

打赏作者

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

抵扣说明:

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

余额充值