写了一个关于判断文件是否存在的递归函数(SHELL)
业务背景
当在一台linux的服务器上,要获取数据文件时,这个文件是从其他linux服务器定时同步过来的。
此时,我们应该要判断这个文件是否存在,如果不存在,相隔多久后再去判断一次,直到存在,就
退出。
用SHELL脚本去实现以上的业务场景,只需要一个递归函数就可以做到;下面是具体的脚本实现。
#!/bin/sh #detemining file is exist,if not,10s later determining one more time #if had out #filename's variable filePath='/home/hadoop/Templates/tmp.txt' tmpnum=1 function touchfileisexist() { #if the variable tmpnum lese than 4 do,else out while [[ "$tmpnum" -le 4 ]];do #if the file is exist write log and exit out,else stop 5s and Call this functions by seft if [ -f "$filePath" ];then echo "$filePath is exist" >> /home/hadoop/Templates/tmp.log exit 1 else echo "$filePath is not exist" >> /home/hadoop/Templates/tmp.log sleep 5 tmpnum=$(($tmpnum+1)) touchfileisexist fi done exit 1 } touchfileisexist

浙公网安备 33010602011771号