70

I have below code in my shell script which will keep on sleeping if it doesn't finds any file. And it sleeps for half an hour but currently I don't have any counter like only execute the below code 20 times and then exit the program if the files are still are not there (means don't do anything after 20 checks and exit the full script).

What's the best way to do this problem? So that I am also aware by looking at the emails that it has tried 20 times.

Hope I am clear enough.

while true; do
  if /home/hadoop/latest/bin/hadoop fs -ls /apps/hdtech/bds/quality-rt/dt=$DATE_YEST_FORMAT2 then
       echo "Files Present" | mailx -s "File Present"  -r admin@host.com admin@host.com
       break
  else
       echo "Sleeping for half an hour" | mailx -s "Time to Sleep Now"  -r admin@host.com admin@host.com
       sleep 1800
  fi
done
4

4 回答 4

119

Here's how you might implement a counter:

counter=0
while true; do
  if /home/hadoop/latest/bin/hadoop fs -ls /apps/hdtech/bds/quality-rt/dt=$DATE_YEST_FORMAT2 then
       echo "Files Present" | mailx -s "File Present"  -r admin@host.com admin@host.com
       exit 0
  elif [[ "$counter" -gt 20 ]]; then
       echo "Counter: $counter times reached; Exiting loop!"
       exit 1
  else
       counter=$((counter+1))
       echo "Counter: $counter time(s); Sleeping for another half an hour" | mailx -s "Time to Sleep Now"  -r admin@host.com admin@host.com
       sleep 1800
  fi
done

Some Explanations:

  • counter=$((counter+1)) - this is how you can increment a counter. The $ for counter is optional inside the double parentheses in this case.
  • elif [[ "$counter" -gt 20 ]]; then - this checks whether $counter is not greater than 20. If so, it outputs the appropriate message and breaks out of your while loop.
于 2012-11-30T03:44:21.227 回答
26

尝试这个:

counter=0
while true; do
  if /home/hadoop/latest/bin/hadoop fs -ls /apps/hdtech/bds/quality-rt/dt=$DATE_YEST_FORMAT2 then
       echo "Files Present" | mailx -s "File Present"  -r admin@host.com admin@host.com
       break
  elif [[ "$counter" -gt 20 ]]; then
       echo "Counter limit reached, exit script."
       exit 1
  else
       let counter++
       echo "Sleeping for another half an hour" | mailx -s "Time to Sleep Now"  -r admin@host.com admin@host.com
       sleep 1800
  fi
done

解释

  • break- 如果文件存在,它将中断并允许脚本处理文件。
  • [[ "$counter" -gt 20 ]]- 如果计数器变量大于 20,脚本将退出。
  • let counter++- 每次通过时计数器加 1。
于 2012-11-30T07:52:34.227 回答
1

您可以使用for循环而不是while

max_loop=20
for ((count = 0; count < max_loop; count++)); do
  if /home/hadoop/latest/bin/hadoop fs -ls /apps/hdtech/bds/quality-rt/dt=$DATE_YEST_FORMAT2 then
       echo "Files Present" | mailx -s "File Present"  -r admin@host.com admin@host.com
       break
  else
       echo "Sleeping for half an hour" | mailx -s "Time to Sleep Now"  -r admin@host.com admin@host.com
       sleep 1800
  fi
done

if [ "$count" -eq "$max_loop" ]; then
  echo "Maximum number of trials reached" >&2
  exit 1
fi
于 2021-02-16T01:17:35.833 回答
0
#!/usr/bin/env bash
counter=0
while [[ "$counter" -lt 20 ]] 
do
  if [[ $counter -gt 0 ]] ; then
    echo "Counter: $counter time(s); Sleeping for another half an hour" | mailx -s "Time to Sleep Now"  -r admin@host.com admin@host.com
    sleep 1800  
  fi
  if /home/hadoop/latest/bin/hadoop fs -ls /apps/hdtech/bds/quality-rt/dt=$DATE_YEST_FORMAT2 ; then
       echo "Files Present" | mailx -s "File Present"  -r admin@host.com admin@host.com
       exit 0
  fi
  : $((counter++))
done

echo "Counter: $counter times reached; Exiting loop!"
exit 1

这是对此的另一种看法。我添加了这段代码来说明这些要点:

  1. 可以将while您的计数器作为条件 - 这消除了检查循环中变量值的需要。
  2. 由于 if 分支会导致循环结束(通过 break 或 exit),因此不需要 final else
  3. 我提供了不同的语法来增加计数器。老实说,我从未见过 koola 的版本(let counter++)但我非常喜欢它(它对我来说更简单)。
  4. 我不是 100% 确定某些 while 循环提供的答案实际上循环了 20 次。相反,我认为其中一些循环了 21 次。
  5. 所提出的解决方案实际上给管理员发送了额外的时间 - 如果您要退出,则无需告诉管理员您要睡半个小时。我在 while 循环的开头添加了一个额外的 if 以避免额外的电子邮件(和睡眠)。

这是 20 次找不到文件的运行的输出:

Counter: 1 time(s); Sleeping for another half an hour
Counter: 2 time(s); Sleeping for another half an hour
...
Counter: 19 time(s); Sleeping for another half an hour
Counter: 20 times reached; Exiting loop!

if 的短处是我们检查文件 20 次,但只休眠 19 次。

于 2021-07-23T16:33:32.837 回答