Array problem in Bash Script

I am trying to write a Bash Script using a couple of arrays. I need to perform a countdown of sorts on an array done once daily, but each day would start with the numbers from the previous day. This is what I'm starting with :

#!/bin/bash

days=(9 8 7 6 5)

for (( i = 0 ; i < ${#days[@]} ; i++ ))
  do (( days[$i]=${days[$i]} - 1 )) 
  done 

This leaves me with this :

$ 8 7 6 5 4

How do I start with days=(8 7 6 5 4) the next time the script is run instead of days=(9 8 7 6 5) ?

I am using Linux Mint 18 (Cinnamon) and the latest version of Bash

Thank You in advance for any and all suggestions.
Cogiz

Your current code explicitly initializes the array using:

days=(9 8 7 6 5)

If you want to update the array and load those updated values the next time you run your script, you need to save the updated values in a file before you exit your script, and when you run your script you need to load the array from that file; not with the initialization step shown above.

Note that you also need to be sure that you never run more than one copy of your script at a time. If you have two copies of the script running concurrently, you have to be absolutely sure that they destroy the file saving your stored data by performing concurrent updates to that file.

1 Like

Why don't you create the array dynamically, by e.g. using the output of seq ? You'd still need to keep the starting value somewhere.

What kind of logics do you wish to apply when the days reach zero? Continue into the negative numbers? Reset to sth. like 30/31?

1 Like

Thank you for suggestions. I am writing a little script that will keep track of current medications (dosage, supply, refills, etc.) and send an email when it is time for the prescription to be refilled, e.g. 30 day supply with 3 refills: when 30 gets to 0 it will reset
to 30 and refills will decrease to 2. I will try saving to file then reload upon next execution of script. I will post if successful or not. Thanks again.
Cogiz

I would like to officially thank all the people who offered advice on my problem. I can say for sure that the problem has been solved by populating an empty array from a txt file and saving output to same txt file for use when the script is run next time.

best regards to all,
Cogiz