bash 在 shell 脚本中添加计数器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13638670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:02:16  来源:igfitidea点击:

Adding Counter in shell script

bashshell

提问by arsenal

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).

我的 shell 脚本中有以下代码,如果找不到任何文件,它将继续休眠。它睡了半个小时,但目前我没有任何计数器,例如只执行下面的代码 20 次,然后如果文件仍然不存在则退出程序(意味着在 20 次检查后不做任何事情并退出完整脚本)。

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.

解决这个问题的最佳方法是什么?这样我也可以通过查看它尝试了 20 次的电子邮件来了解。

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 [email protected] [email protected]
       break
  else
       echo "Sleeping for half an hour" | mailx -s "Time to Sleep Now"  -r [email protected] [email protected]
       sleep 1800
  fi
done

回答by sampson-chen

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 [email protected] [email protected]
       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 [email protected] [email protected]
       sleep 1800
  fi
done

Some Explanations:

一些解释:

  • counter=$((counter+1))- this is how you can increment a counter. The $for counteris optional inside the double parentheses in this case.
  • elif [[ "$counter" -gt 20 ]]; then- this checks whether $counteris not greater than 20. If so, it outputs the appropriate message and breaks out of your while loop.
  • counter=$((counter+1))- 这是增加计数器的方法。该$用于counter在这种情况下,双括号内可选的。
  • elif [[ "$counter" -gt 20 ]]; then- 这将检查是否$counter不大于20。如果是这样,它会输出适当的消息并跳出你的 while 循环。

回答by koola

Try this:

尝试这个:

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 [email protected] [email protected]
       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 [email protected] [email protected]
       sleep 1800
  fi
done

Explanation

解释

  • break- if files are present, it will break and allow the script to process the files.
  • [[ "$counter" -gt 20 ]]- if the counter variable is greater than 20, the script will exit.
  • let counter++- increments the counter by 1 at each pass.
  • break- 如果文件存在,它将中断并允许脚本处理文件。
  • [[ "$counter" -gt 20 ]]- 如果计数器变量大于 20,脚本将退出。
  • let counter++- 每次通过将计数器加 1。