Setting a TZ variable in a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Setting a TZ variable in a script
# 1  
Old 10-31-2011
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.

Code:
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

Code:
#!/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

# 2  
Old 10-31-2011
Try this:

Code:
#!/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.
# 3  
Old 10-31-2011
Hey,

Thanks for the reply.

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

Code:
./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

# 4  
Old 10-31-2011
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:

Code:
# /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.. :-(
This User Gave Thanks to dude2cool For This Post:
# 5  
Old 10-31-2011
Humm your right.... must have a typo somewhere.

Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Setting a permanent global variable in unix accessible from any script

Is there anyway in which i can set a permanent global variable in unix, which when initialised with a value and modified during any shell script, would retain its value even if i logout and login I dont know whether i am being able to express my need clearly but basically what i want is a... (3 Replies)
Discussion started by: arindamlive
3 Replies

2. Shell Programming and Scripting

setting a shell script variable in awk

The following is part of a larger shell script grep -v "Col1" my_test.log | grep -v "-" | awk '$5 == "Y" {print $1}' instead of printing, can I set set $1 to a variable that the rest of the shell script can read? if $5 == Y, I want to call another shell script and pass $1 as a... (2 Replies)
Discussion started by: guessingo
2 Replies

3. Shell Programming and Scripting

Setting a variable in a while loop (.ksh script)

Hello Everyone, I'm still trying to grasp many concepts in .ksh scripting, one of them being variables inside loops. My problem is the following: * I'm trying to set a variable inside a while read loop to reuse it outside of said loop. My lines are the following :... (13 Replies)
Discussion started by: jimmy75_13
13 Replies

4. Shell Programming and Scripting

Setting environment variable using shell script

Hi All, I'm trying to write an menu driven program to automate some functions which involve loging to multiple hosts. The hosts can differ for every use, so I thought I would use an config file to get the hostnames. Now I need to set those values in the config file to environment variable to... (6 Replies)
Discussion started by: arun_maffy
6 Replies

5. Shell Programming and Scripting

Help with setting a variable!

I am working within a while loop and i am trying to set a variable that will read out each count of the files. the problem is the count variable i have set up gives me a total and not the individual count of each file. in the data area there is 4 abc.dat and 1 def.dat. how can i do this??? ... (2 Replies)
Discussion started by: Pablo_beezo
2 Replies

6. Shell Programming and Scripting

Setting environment variable on a remote solaris machine using shell script

Hi, I am trying to set environment variable on a remote machine. I want to do it by running a shell script Here's what I am doin rsh <remote-hostname> -l root "cd /opt/newclient; . ./setp.sh" In setp.sh, I have ############################# cd ../newlib; export... (1 Reply)
Discussion started by: eamani_sun
1 Replies

7. UNIX for Dummies Questions & Answers

setting a global variable in script

Hi All, I know to set global variable i can use export .. But take the situation like below .. I want to set a variable in one script and access that in second script i have done like this .. It is not working one.sh #!/usr/bin/ksh echo $RISSHI export RISSHI=1 two.sh... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

8. Shell Programming and Scripting

Setting basename and dirname variable to simply script.

Hello all, Can somebody explain to me how set up a basename and dirname variable to simplify this script. I currently have a 'infile' with the contents of FTTPDataPVC_ & BaaisDSLFeed. I need to add a basename and or dirname variable so that any additions can be made through the infile and not... (1 Reply)
Discussion started by: liketheshell
1 Replies

9. UNIX for Dummies Questions & Answers

Setting a variable

I want to set a variable to be any number of dashes. Rather than doing the following: MYVAR="------------------" I'd like to be able to set the variable to, say, 80 dashes but don't want to have to count 80 dashes. Is there a way to do this? (2 Replies)
Discussion started by: photh
2 Replies

10. Programming

setting CC variable

I am trying to install GCC-3.1.1 on an SGI Indigo2. I already have MIPSpro 7.2.1 installed. However, when I try to configure GCC-3.1.1, I get the message "cc ERROR: cc -o conftest -g failed, You must set the environment variable CC to a working compiler." What is the name of the MIPSpro c++... (1 Reply)
Discussion started by: mdbanas
1 Replies
Login or Register to Ask a Question