在指定时间内不停刷新目录下文件的时间戳

前端之家收集整理的这篇文章主要介绍了在指定时间内不停刷新目录下文件的时间戳前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

#!/bin/sh
#Usage : run script under certain directory,to keep files under this directory fresh
#Example: nohup keepAlive 1.5&
#Note: directory should have permission to write files under current directory

#Visit given directory recursively and use touch command to update the timestamp of all files in it
function freshDir() {
	for file in `ls $1`
	do
        touch $1"/"$file
		if [ -d $1"/"$file ]
		then
			freshDir $1"/"$file
		fi
	done
}

#Show help when script started without arguments
function showHelp() {
	echo "Run script under certain directory,to keep files under this directory up to date and not be deleted"
	echo "Example: nohup ~yantang/tools/keepAlive 1.5&"
	echo "Then all files under current directory will be refreshed in the next 1.5 days "
	echo "Kill it manually when you no longer need it"
}

if [ $# -gt 0 ]
then
	daysAlive=$1
else
	showHelp
	exit
fi

curDate=`date`
echo "The time now: $curDate"
echo "The directory will be alive for $daysAlive days"

startTime=`date +%s`
currentTime=$startTime
typeset days=$(echo ${currentTime} ${startTime}|awk '{print ($1-$2)/86400 }')

isAlive=1

while [ $isAlive -gt 0 ]
do
	freshDir "."
	sleep 600    #Sleep 10 minutes
	currentTime=`date +%s`
	days=$(echo ${currentTime} ${startTime} | awk '{print ($1-$2)/86400 }')
	isAlive=$(echo ${days} ${daysAlive}|awk '{if($1<$2) print 1; else print 0;}')
done

curDate=`date`
echo "Now the time is $curDate .keepAlive stopped running. Bye"

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

猜你在找的Shell相关文章