0

I was wondering the best way to implement a counter in bash based on the date. For example if todays date is 5-26-17, a counter will increment every time my bash script is run (starting at 0). So, if the script is run 24 times (once per hour) the counter will be at 24. Now we roll over to the next day 5-27-17, I want to reset the counter to 0 and start incrementing every time the script is run.

My first thought on solving this with a bash script is to run the date utility and create a file based on the current date. After the file is created, using the file to track the counter value. Is there a better way of implementing this?

What I'm trying to do basically is synonymous with using a key value pair. The key is today's date, and the value is the number of times the script has run.

My Solution:

# Create a counter file to keep track of how many times the script
# was run each day.
  DATE_COUNTER_FILE=$(date "+%m-%d-%Y").counter
  COUNTER=1;
  if [ -f $DATE_COUNTER_FILE ]; then
    COUNTER=`cat $DATE_COUNTER_FILE`
    ((COUNTER+=1))
    echo $COUNTER > $DATE_COUNTER_FILE
  else
    touch $DATE_COUNTER_FILE
    echo $COUNTER > $DATE_COUNTER_FILE
  fi;
Liver
  • 363
  • 1
  • 3
  • 19
  • 3
    Sorry, but StackOverflow is about helping people fix their programming code. Requests for program design tips, tutorials, research, tools, recommendations, libraries, and code are off-topic. ***Please*** read http://stackoverflow.com/help/how-to-ask , http://stackoverflow.com/help/dont-ask , http://stackoverflow.com/help/mcve and take the [tour](http://stackoverflow.com/tour) before posting more Qs here. Post sample input, or starting values, expected values after processing from same inputs, any attempt to solve the problem as code and people are likely to help you improve it. Good luck. – shellter May 26 '17 at 14:43
  • BTW, the primary gotcha you'll run into here is file locking -- a naive implementation of the logic you're proposing would be prone to race conditions. See, ie. https://stackoverflow.com/a/17480020/14122 for a good example of using `flock` for locking. Otherwise, you're generally on the right track already. – Charles Duffy May 26 '17 at 14:48
  • What topics can I ask about here? - a specific programming problem (check) - a software algorithm (check) - software tools commonly used by programmers (check, I was looking for a possible *NIX utility) a practical, answerable problem that is unique to software development (check) Then you’re in the right place to ask your question! – Liver May 30 '17 at 14:51

1 Answers1

2

Not sure if something like this is helpful to you:

#!/bin/bash
if [$day == ""]; then day=$(date +"%d"); fi
if [$month == ""]; then month=$(date +"%m"); fi
if [$year == ""]; then year=$(date +"%Y"); fi
# ...
# DO YOUR STUFF WITH NUMBERS
# ...
export day=${day}
export month=${month}
export year=${year}

The first part store date data in variables but only if the variable is not set yet. Then you do what you want with simple math (use expr for this purpose). Then you export variables as environment variables so that they can be found on the next execution of the script.

If you need to keep the data alive even after a shutdown or reboot, you need to export the data to file.

If you need to reset the data every day you can add a variable (I called it today) and make the script like this:

#!/bin/bash
if ([ "$today" != "" ] && [ "$today" != $(date +"%d") ]); then day="" && month="" && year=""; fi
if [ "$day" == "" ]; then day=$(date +"%d") && today=$(date +"%d"); fi
if [ "$month" == "" ]; then month=$(date +"%m"); fi
if [ "$year" == "" ]; then year=$(date +"%Y"); fi
# ...
# DO YOUR STUFF WITH NUMBERS
# ...
export day=${day}
export month=${month}
export year=${year}

This way the script will reset the variables $day $month and $year each day.

La faguette
  • 541
  • 4
  • 11