shell 用技巧

1:查看一个目录是否已经mount --bind

执行 mountpoint -q /test/mount
echo $? 如果是0表示已经mount

1.1 根据mount上否进行程序启动

#!/bin/bash
file_path0=$(cd $(dirname $0); pwd)
file_path1=${file_path0}/HSRPD/logger/check.txt
function  kill_detect(){
	file=${file_path0}/HSRPD/detect_pid_list.txt
    kill $(cat $file)
}
function start_detect(){
   gnome-terminal -- /usr/bin/python3  $file_path0/HSRPD/main_HSRPD.py & 
}
function whether_changed(){
	echo $file_path1
    file_old_stat1=$(stat -c %y $file_path1)
   
    while [[ true ]]; do
	 mountpoint -q /home/lxkj/cifsshare
   	 if [ $? -eq 0 ];then
		sleep 20
		if [ -f "$file_path1" ]; then
		    file_new_stat1=$(stat -c %y  $file_path1)
				echo $file_new_stat1
		    if [ "echo $file_old_stat1" =  "echo $file_new_stat1" ]; then
		        echo "### In ${check_time}s ,${file_path1} does not change ###"
		        kill_detect
			sleep 5 
		        start_detect
		    else
		        echo $file_old_stat1
		        file_old_stat1=$file_new_stat1
		        echo "file change $file_old_stat1"
		    fi
		else
		   echo "file hardware.prom no exit "
		fi
	else
		kill_detect		
		echo "dir not mount "
		echo '123' | sudo -S mount -t cifs //192.168.0.70/ssd /home/lxkj/cifsshare -o username=root,password=root
		sleep 5		
		continue
	fi
     done
}
function start_prg(){
	mountpoint -q /home/lxkj/cifsshare
	if [ $? -eq 0 ];then
		whether_changed
	else
		echo '123' | sudo -S mount -t cifs //192.168.0.70/ssd /home/lxkj/cifsshare -o username=root,password=root
		whether_changed
	fi
}
start_prg

2: 批量删除文件

#!/bin/bash

#移除/删除文件路径
removeFolder="/home/lxkj/cifsshare"
dir="/home/lxkj/cifsshare"

#输入待删除文件的日期 ./deleteZip.sh 20191212
removedate=$1

#判断位置参数是否为空
#if [ $# -ne 1 ];then             

#  echo "Usage $0 removedate"
#  exit
#fi
#echo "removedate: $removedate"

#获取文件夹下面所有文件名称
function  kill_detect(){
files=$(ls $dir)
#echo $files

#循环遍历删除匹配的文件
for file in ${files[*]}
do
	#echo $file

	# 删除压缩包文件
	if [[ $file = $removedate*.enc ]];then             
	  echo "delete $dir/$file"
	  rm -rf $dir/$file
	  #echo "remove $dir/$file to $removeFolder"
	  #mv ./$file $removeFolder/.
	  # >> del$removedate.log
	fi

	# 删除excel文件
	if [[ $file = $removedate*.txt ]];then             
	  echo "delete $dir/$file"
	  rm -rf $dir/$file
	  #echo "remove $dir/$file to $removeFolder"
	  #mv ./$file $removeFolder/.
	  # >> del$removedate.log
	fi

done
	echo "delete success!"
}
while true
do
kill_detect
sleep 10
done

おすすめ

転載: blog.csdn.net/qq_51609636/article/details/120789479