Stephen Adams
Stephen Adams

Reputation: 11

Comparing two timezones

Background: I'm on a team that deploys software to servers in over 30 different timezones. Each server location has it's own maintenance window. Our SysAd team is in 5 different locations. Currently we use an internal wiki page with a table of server locations and google time conversion links like this. So our internal wiki page of maintenance windows is full of timezones of the servers, if they follow DST offset or not and then a conversion link for the start time and another google conversion link for the end time of the maintenance window. It's time consuming keeping track of which servers can be patched right now. As some servers are in the same timezone as other server but maybe doesn't follow DST or not it's a mess of keeping track. Some of the Software Devs at work keep a full bookmark folder of all of the conversion links and I wanted to send them a script that can simplify this.

Goal: I basically want a script to be able to compare the SysAds current timezone to the timezone of the different servers. In the end I want an echo statement of which server can be patched right now, and eventually I want to add further logic later that will asses the time until next maintenance window and that servers location.

Problem #1: I'm new to scripting. Problem #2: When doing what I thought was a simple TZ comparison it's not outputting what I expect. I feel once I understand why the output below is as such I think I can figure out the rest of the logic. I don't necessarily care about specific hours and minutes at this point. Just why is the If statement not outputting "the same".

Desired script:

  #!/bin/bash
  ##SysAd Sets his own timezone in the first TZ variable.                                                                                                                                                                                                                                                                                                                 
  export TZ=America/Los_Angeles                                                                                                                                                                                                                                                                                               
  TZ1=`date +%::z`                                                                                                                                                                                                                                                                                                                  
  echo $TZ1                                                                                                                                                                                                                                                                                                                   

  ##Timezone of Server in location 1                                                                                                                                                                                                                                                                                                                             
  TZ=America/Los_Angeles                                                                                                                                                                                                                                                                                                      
  TZ2=`date +%::z`                                                                                                                                                                                                                                                                                                                  
  echo $TZ2                                                                                                                                                                                                                                                                                                                   

  if [[ " TZ1 " == " TZ2 " ]]; then                                                                                                                                                                                                                                                                                           
      echo "the same"                                                                                                                                                                                                                                                                                                         
  else                                                                                                                                                                                                                                                                                                                        
      echo "not the same"

output:

% ./test.sh 
-08:00:00
-08:00:00
not the same

expected output is that they are the same. echo'ing out both the TZ1 and TZ2 variable are both "-08:00:00"

Upvotes: 0

Views: 319

Answers (1)

Akshay G Bhardwaj
Akshay G Bhardwaj

Reputation: 339

It should be:

if [[ "$TZ1" == "$TZ2" ]]

Upvotes: 1

Related Questions