1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > linux shell脚本判断文件或文件夹是否存在循环操作

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

时间:2019-06-29 08:09:49

相关推荐

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.logfidone

#shell判断文件夹是否存在

#如果文件夹不存在,创建文件夹

if [ ! -d “/Top” ]; then

mkdir -p /Topfi

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

folder="/Top"

file="/Top/test.txt"

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

if [ ! -x “folder"];thenmkdir"folder"]; then mkdir "folder"];thenmkdir"folder”

fi

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

if [ ! -d “folder"];thenmkdir"folder"]; then mkdir "folder"];thenmkdir"folder”

fi

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

if [ ! -f “file"];thentouch"file" ]; then touch "file"];thentouch"file”

fi

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

if [ ! -n “file"];thenecho"file" ]; then echo "file"];thenecho"file 变量为空!”

exit 0

fi

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

if [ “file1"="file1" = "file1"="file2” ]; then

echo “$file1 equal file2"elseecho"file2" else echo "file2"elseecho"file1 not equal $file2”

fi

#!/bin/sh

说明:判断文件是否存在

myPath="/Top"

myFile="/Top/access.log"

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

if [ ! -x “myPath"];thenmkdir"myPath"]; then mkdir "myPath"];thenmkdir"myPath”

fi

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

if [ ! -d “myPath"];thenmkdir"myPath"]; then mkdir "myPath"];thenmkdir"myPath”

fi

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

if [ ! -f “myFile"];thentouch"myFile" ]; then touch "myFile"];thentouch"myFile”

fi

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

if [ ! -n “myVar"];thenecho"myVar" ]; then echo "myVar"];thenecho"myVar 变量为空!”

exit 0

fi

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

if [ “file1"="123123"];thenecho"file1" = "123123" ]; then echo "file1"="123123"];thenecho"file1 equal file2"elseecho"file2" else echo "file2"elseecho"file1 not equal $file2”

fi

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。