I want to create a BASH script that outputs the following:
Chile: 03:46am, Friday, 27 September, 2024
Tierra Del Fuego: 03:46am, Friday, 27 September, 2024
Falkland Islands: 03:46am, Friday, 27 September, 2024
South Georgia and the South Sandwich Islands: 04:46am, Friday, 27 September, 2024
UTC: 06:46am, Friday, 27 September, 2024
South Africa: 08:46am, Friday, 27 September, 2024
Port Alfred, Iles Crozet: 10:46am, Friday, 27 September, 2024
Western Australia: 14:46pm, Friday, 27 September, 2024
South Australia: 16:16pm, Friday, 27 September, 2024
VIC, NSW, ACT, and QLD, Australia: 16:46pm, Friday, 27 September, 2024
New Zealand: 18:46pm, Friday, 27 September, 2024
I have managed to do this so far using the following code:
#Chile
location="America/Santiago"
label="Chile"
time=$(TZ=$location date "+%H:%M%p")
date=$(TZ=$location date "+%A, %d %B, %Y")
echo "$label: $time, $date"
…and then just having multiple blocks of that same code, with location and label changed as necessary.
This is ok, but it’s a 103-line file. I want to learn how to use associative arrays to achieve the same task for no reason other than simply to learn.
I have created the array itself as follows:
# Declare an associative array
declare -A locationsArray
# Add elements to the associative array
locationsArray[America/Santiago]="Chile"
locationsArray[America/Argentina/Ushuaia]="Tierra Del Fuego"
locationsArray[Atlantic/Stanley]="Falkland Islands"
locationsArray[Atlantic/South_Georgia]="South Georgia and the South Sandwich Islands"
locationsArray[Etc/UTC]="UTC"
locationsArray[Africa/Johannesburg]="South Africa"
locationsArray[Asia/Dubai]="Port Alfred, Iles Crozet"
locationsArray[Australia/Perth]="Western Australia, Australia"
locationsArray[Australia/Adelaide]="South Australia, Australia"
locationsArray[Australia/Melbourne]="VIC, NSW, ACT, and QLD, Australia"
locationsArray[Pacific/Auckland]="New Zealand"
Now I just need to be able to take a key from the array and its associated value (e.g. “America/Santiago” and “Chile”), and iterate through the entire array using the above code as a template.
How can I go about doing this?
Thank you.