Setting a TZ variable in a script

Hello all,

I know this must be simple .... but i can't grasp what could be the issue.

I'm trying to setup the timezone variable (to the unix command date) according to what i find in a value that i got from parsing the config file.

The end result would be setting the log file with this new value.

RHEL 6

Those are not the REAL values of course. Its only for testing.

bash$ cat time_zone.tmp
NPK TZ=/usr/share/zoneinfo/Asia/Hong_Kong
ATHEX TZ=/usr/share/zoneinfo/Canada/Eastern
HSBC TZ=/usr/share/zoneinfo/Asia/Hong_Kong
#!/bin/sh

LIST="HSBC ATHEX NPK CLSA ETC"

for ITEM in ${LIST} ; do
        TZ=`grep ${ITEM} time_zone.tmp | cut -d " " -f2`
        if [ X"${TZ}" != X ] ; then
                DATE=`${TZ}; date`
        else
                DATE=`date`
        fi
        echo ${ITEM} ": " ${DATE}
done

Try this:

#!/bin/sh

LIST="HSBC ATHEX NPK CLSA ETC"

for ITEM in ${LIST} ; do
        TZ=`grep ${ITEM} /tmp/time_zone.tmp | cut -d "=" -f2`
        if [ X"${TZ}" != X ] ; then
               DATE=`TZ=${TZ};export TZ;date`
        else
                DATE=`date`
        fi
        echo ${ITEM} ": " ${DATE}
done

Assuming your time_zone.tmp is in /tmp. Modify that path to match where ever your time_zone.tmp is.

Hey,

Thanks for the reply.

Tried that already with the = in the cut and just passing the path name in the TZ variable.

./time_zone
export: 18: /usr/share/zoneinfo/Canada/Eastern: bad variable name
export: 18: /usr/share/zoneinfo/Asia/Hong_Kong: bad variable name
export: 18: /usr/share/zoneinfo/Asia/Hong_Kong: bad variable name

Hmm, unless they changed the way you set TZ in rhel 6, I just tried it on rhel 5.x and it works for me, here is the output from the script I gave above:

# /tmp/12.sh
HSBC :  Tue Nov 1 01:51:05 HKT 2011
ATHEX :  Mon Oct 31 13:51:05 EDT 2011
NPK :  Tue Nov 1 01:51:05 HKT 2011
CLSA :  Mon Oct 31 13:51:05 EDT 2011
ETC :  Mon Oct 31 13:51:05 EDT 2011

I don't have a rhel 6 to try, sorry.. :frowning:

1 Like

Humm your right.... must have a typo somewhere.

Thanks!