Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Counter in shell script

Tags:

bash

shell

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 [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 
like image 291
arsenal Avatar asked Nov 30 '12 03:11

arsenal


People also ask

How do you create a counter in bash?

Use let command to count increment The foremost step is to create a bash file. For that, you can use any command-line editor and paste the below code and make sure to save it. We have created the “COUNTER” variable to store increment value which will be invoked by let or Bash Arithmetic Operations.

What is $() in bash script?

$() Command Substitution According to the official GNU Bash Reference manual: “Command substitution allows the output of a command to replace the command itself.

How do I count lines in bash?

wc. The wc command is used to find the number of lines, characters, words, and bytes of a file. To find the number of lines using wc, we add the -l option. This will give us the total number of lines and the name of the file.


2 Answers

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 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.
like image 171
sampson-chen Avatar answered Oct 03 '22 13:10

sampson-chen


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.
like image 35
koola Avatar answered Oct 03 '22 14:10

koola