Answers:
该bash函数将一直阻塞,直到出现给定的文件或达到给定的超时为止。如果文件存在,则退出状态为0;否则,退出状态为0。如果不是,则退出状态将反映该功能等待了几秒钟。
wait_file() {
local file="$1"; shift
local wait_seconds="${1:-10}"; shift # 10 seconds as default timeout
until test $((wait_seconds--)) -eq 0 -o -f "$file" ; do sleep 1; done
((++wait_seconds))
}
这是如何使用它:
# Wait at most 5 seconds for the server.log file to appear
server_log=/var/log/jboss/server.log
wait_file "$server_log" 5 || {
echo "JBoss log file missing after waiting for $? seconds: '$server_log'"
exit 1
}
另一个例子:
# Use the default timeout of 10 seconds:
wait_file "/tmp/examplefile.txt" && {
echo "File found."
}
start=`date +%s`; while (( `date +%s` - start > 10 )); do sleep 1; done