The linux shell script judges whether the file or folder has a loop operation

If you change the script I wrote recently, you can use
3.sh all the data pointing to the directory like /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 Determine whether the folder exists

#If the folder does not exist, create a folder
if [! -D “/Top” ]; then
mkdir -p /Topfi

#shell Determine whether the file or directory exists or has permission

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

The -x parameter determines whether $folder exists and has executable permissions

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

The -d parameter determines whether $folder exists

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

The -f parameter determines whether $file exists

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

-n judge whether a "variable" has a value

if [ ! -n “ f i l e " ] ; t h e n e c h o " file" ]; then echo " file"];t h e n e c h o "The file variable is empty!"
exit 0
fi

Determine whether the string content of two variables are the same

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

Description: Determine whether the file exists

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

The -x parameter here determines whether $myPath exists and has executable permissions

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

The -d parameter here determines whether $myPath exists

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

The -f parameter here determines whether $myFile exists

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

Other parameters include -n, -n is to determine whether a variable has a value

if [ ! -n “ m y V a r " ] ; t h e n e c h o " myVar" ]; then echo " myVar"];t h e n e c h o " myVar variable is empty!"
exit 0
fi

Determine whether the content of the $file string is the same as "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

Guess you like

Origin blog.csdn.net/weixin_44578029/article/details/111229980