我正在创建一个脚本,该脚本应该等到某个文件(例如stop.dat
)出现或者在某段时间(例如500秒)过后。
我知道如何等到文件出现:
while [ ! -f ./stop.dat ]; do
sleep 30
done
如何在while循环中添加其他语句?
答案 0 :(得分:2)
如果您想这样做,那么您可以执行以下操作:
nap=30; slept=0
while [ ! -f ./stop.dat ] && ((slept<500)); do
sleep $nap;
slept=$((slept+nap))
done
使用inotifywait而不是轮询将是一种更合适的方式。
答案 1 :(得分:1)
你可以记住时间并与循环条件中的当前时间进行比较
echo -n "before: "
date
t1=$(( $(date +"%s" ) + 15 )) #15 for testing or … your 500s later
while [ $(date +"%s") -lt $t1 ] #add other condition with -a inside the [ ] which is shortcut for `test` command, in case you want to use `man`
do
sleep 3 #do stuff
done
echo -n "after: "
date