我希望能够在Linux shell(bash)上运行一段脚本并持续一段时间。我写了类似下面的代码片段。但是,我看到只有睡眠正确发生,但执行在第一次执行后停止。
#!/bin/bash
some_work(){
echo "Working for $1 minutes"
if [ $1 -gt 0 ]
then
echo "Setting timer for $1 minutes"
run_end_time=$(($1 * 60))
start_time=SECONDS
curr_time=SECONDS
while $((curr_time < $((start_time + run_end_time)) )) :
do
#Do some work here
curr_time=SECONDS
done
fi
}
sleep_time(){
echo "Sleeping for $1 minutes"
sleep $(($1 * 60))
}
if [ $# -gt 1 ]
then
echo "Starting Steeplechase run for $1/$2"
while :
do
some_work $1
sleep_time $2
done
fi
我得到的回复是./script.sh:30:1:找不到。也许我在这里错过了一些重要的事情。
答案 0 :(得分:0)
一些问题:
while (( ... )); do
而不是while $(( ... )); do
行尾的冒号
while $((curr_time < $((start_time + run_end_time)) )) :
一定不能在那里。
SECONDS
;分配应该看起来像var=$SECONDS
而不是var=SECONDS
。完整的脚本,有一些建议和我的缩进想法:
#!/bin/bash
some_work () {
echo "Working for $1 minutes"
if (( $1 > 0 )); then
echo "Setting timer for $1 minutes"
run_end_time=$(($1 * 60))
start_time=$SECONDS
curr_time=$start_time # Want this to be the same value
while ((curr_time < $((start_time + run_end_time)) )); do
#Do some work here
curr_time=$SECONDS
done
fi
}
sleep_time () {
echo "Sleeping for $1 minutes"
sleep $(($1 * 60))
}
if (( $# > 1 )); then
echo "Starting Steeplechase run for $1/$2"
while true; do
some_work $1
sleep_time $2
done
fi