3

I am facing an issue with a bash script that I am writing. I am pretty new to bash scripts so I would be glad for some help or inputs.

We are running an Appliance Server. Unfortunately, I am not allowed to change the timezone of this server. So I need to work with the UTC Timezone.

Now I wanted to create script that should run automatically in our maintenance window at 8 PM. Now the problem is, that UTC does not know the difference between summer / winter time.

So I tried the following:

#!/bin/bash
array_time=("Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct")

ZONE=`date | cut -c 5-7`
echo $ZONE

if [[ " ${array_time[@]} " == " ${ZONE} " ]]; then
echo "SUMMERTIME CRONJOB"
else
echo "WINTERTIME CRONJOB"
fi

But for some reason, this is the output:

./test.sh
Jul
WINTERTIME CRONJOB

So even when the value Jul is part of the array_time variable, it goes to the else part of the if condition. I am not sure why this is happening. Anyone got an idea why? Or maybe anyone got a better idea how to solve this?

Thanks for your help

cheers

ArBei
  • 75
  • 1
  • 6

2 Answers2

3

Depending on what is your original goal, you can localy modify the timezone, for example:

TZ=EST date
Tue Jul  5 03:54:39 EST 2016

TZ=EST date: it is setting TZ environment variable to EST, when executing the date command.

You could use either regular expression or fgrep to search the month in an array.

Give a try to this:

#!/bin/bash
array_time=("Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct")

ZONE=$(date "+%b")

if [[ "${array_time[@]}" =~ "${ZONE}" ]] ; then
  printf "SUMMERTIME CRONJOB\n"
else
  printf "WINTERTIME CRONJOB\n"
fi

You could also use a for loop.

Prefer using $(...) to execute command.

It is encouraged to use printf instead of echo:

----

First answer for history:

#!/bin/bash
array_time=("Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct")

ZONE=$(date "+%b")

if printf "%s\n" ${array_time[@]} | fgrep -q "${ZONE}" ; then
  printf "SUMMERTIME CRONJOB\n"
else
  printf "WINTERTIME CRONJOB\n"
fi
Community
  • 1
  • 1
Jay jargot
  • 2,097
  • 1
  • 7
  • 12
  • Hi Jay, thanks for your answer. I just tested it and this is working perfectly. Thank you very much for your fast answer – ArBei Jul 05 '16 at 08:58
  • Thanks for your second answer, will be testing this one in the afternoon. Also thank you for your explanation regarding printf / echo. I am still used to echo, so I will have to change this – ArBei Jul 05 '16 at 10:40
1

Your problem is in:

[[ " ${array_time[@]} " == " ${ZONE} " ]]

which will never be true as ${array_time[@]} will get expanded to:

[[ " Apr May Jun Jul Aug Sep Oct " == " ${ZONE} " ]]

And $ZONE is never equal that string, using the following will work:

[[ " ${array_time[@]} " == *" ${ZONE} "* ]]

Also note that you can use date +'%b' instead of date | cut ...

andlrc
  • 41,466
  • 13
  • 82
  • 108
  • Hi, thanks for your feedback and for showing the reason why my script did not work. Now it is clear to me, why the == ${ZONE} did not work like that. I will test it within the afternoon. Thanks a lot – ArBei Jul 05 '16 at 09:40