Changing TZ from a script


 
Thread Tools Search this Thread
# 1  
Changing TZ from a script

Hi i have a case statement so when the user selects a differnent timezone the TZ value should change, so when another script generates a html page the when date is displayed the time corresponds to the timezone selected. I can't get the TZ to change when using a script but can change the TZ by inputting the code in the terminal:

Code:
echo "Timezone - Choose a Zone - GMT, Asia. "
read Time
case "$Time" in
	GMT)
	unset TZ 
	;;
	Asia)
	echo Time="export TZ='Asia/Kolkata'" >> config.sh
	;;
	*)
	echo "Unknown Time Zone" 
	;;
esac
done

is this possible or what would be an alternative solution for the user to select a timezone from a case statement and change the time displayed via the date command?

thanks
# 2  
If you run the script like ./script (needs to executable, then), it is executed in a subshell and all variables assigned/defined are NOT available in the parent shell.
If you "source" it like . ./script, it is read by and its statement are executed by your current shell, and the variables will be available.
# 3  
Quote:
Originally Posted by RudiC
If you run the script like ./script (needs to executable, then), it is executed in a subshell and all variables assigned/defined are NOT available in the parent shell.
If you "source" it like . ./script, it is read by and its statement are executed by your current shell, and the variables will be available.
I know how to execute the shell scripts, is it possible to hold the TZ in a variable as in my above statement as when I echo out the Time variable it only gets the export anything after the space is not passed?
# 4  
You could put the code into a function, in a script and source that script (or put the function into one of your login scripts, and it will be source automatically during login).

Code:
settz() {
  printf "Enter timezone (Asia, or GMT) [GMT]: "
  read Time
  unset TZ
  case "$Time" in
    Asia)
      export TZ="Asia/Kolkata"
      ;;
    ?*)
      echo "Unknown Time Zone"
      ;;
  esac
}

Code:
$ settz
Enter timezone (Asia, or GMT) [GMT]: 
$
$ echo $TZ
$
$
$ settz
Enter timezone (Asia, or GMT) [GMT]: Asia
$
$ echo $TZ
Asia/Kolkata
$

# 5  
Note that on most systems, unsetting TZ will give you the default time zone set up when the server you're using was installed. Some administrator's may choose GMT, but most systems I've used use the time zone of the location where the server is installed. If you really want to set TZ to GMT (or the ISO convention of UCT) for the current shell and anything it starts, try export TZ=GMT0 (or export TZ=UCT0) instead of unset TZ.

As an example, on the system I'm using now, the commands:
Code:
TZ=UTC0 date;TZ=US/Pacific date;(unset TZ;date)

produce:
Code:
Sat Nov 23 17:16:34 UTC 2013
Sat Nov 23 09:16:34 PST 2013
Sat Nov 23 09:16:34 PST 2013


Last edited by Don Cragun; 11-23-2013 at 07:07 PM.. Reason: Fix typo (UCT s/b UTC).
This User Gave Thanks to Don Cragun For This Post:
# 6  
thanks for the replies i figured it out before the replies but I appreciate you help thanks
# 7  
Quote:
Originally Posted by Don Cragun
... or the ISO convention of UCT ...
Is that a valid synonym for UTC?

Regards,
Alister
This User Gave Thanks to alister For This Post:
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Science: Gadgets
Difficulty: Easy
Microphones can be used not only to pick up sound, but also to project sound similar to a speaker.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Name changing script

I have a problem, and I really need help with this. And I imagine that I will have to learn a new level of scripting to complete this. But I have over 5,000 pictures that are misnamed with the extension .jpg. They're not jpg, they're png, and it's extremely important that they're correctly filed. I... (3 Replies)
Discussion started by: Huitzilopochtli
3 Replies

2. Shell Programming and Scripting

User id who is changing the script.

can we know the user id who changed saved script in cron tab..:D (7 Replies)
Discussion started by: netdbaind
7 Replies

3. Solaris

Changing Passwords with a script.

We are real strict when it comes to passwords. Every 60 days the admins have to change passwords on all of the accounts. And there is pretty strict enforcement of the type of passwords chosen. This is a tedious and monotonous job. Ww don't use NIS or LDAP, so this has to be done on each machine. ... (5 Replies)
Discussion started by: brownwrap
5 Replies

4. Solaris

Help in changing the Parent id of a script

HI I want to run a script as new process. for example, I have two script A and B. From A I am calling B, but When I do ps -ef for the script B the parent ID should not be A' ID. Please help me Thanks & Regards Ramu (3 Replies)
Discussion started by: ramukumar
3 Replies

5. Shell Programming and Scripting

Need help with script changing dates

I am attempting to write a script where the user enters the month and day (two digit format). I am trying to have script will increase 6 more times (totaling 7). I am having issues with the script increasing by one (its either dropping off the lead zero or not increasing for 08 and 09). While... (8 Replies)
Discussion started by: bbraml
8 Replies

6. UNIX for Dummies Questions & Answers

Changing directory with a script

I thought this was as easy as a piece of cake. But it has had me spinning. Here is how my script looks like: When the script finishes and I pwd, I am back to square one. Any help? Thanks in advance. HP-UX hostname B.11.23 U ia64 3308722091 unlimited-user license (5 Replies)
Discussion started by: ron_it_guy
5 Replies

7. Shell Programming and Scripting

Changing script into 2 functions.

ed... always same values get printed out... (2 Replies)
Discussion started by: HardyV2
2 Replies

8. Shell Programming and Scripting

Bash script: issue changing directories in script

I am working on a script that checks two arguments at the command line. The first argument is a search pattern, the second can be a file or a directory, if it is a file a second script is called that checks it for the search pattern. If the second argument is a directory, it checks for the search... (5 Replies)
Discussion started by: Breakology
5 Replies

9. Shell Programming and Scripting

Regarding changing shell thru script

Guys can I change the shell thru script, and after changing i want the script to continue on the previous machine. Or please suggest other alternative if any??? #!/bin/ksh HOST=`hostname` echo "Running the script..." for MyServer in `cat ServerNames.txt` do echo "\n Logging onto... (4 Replies)
Discussion started by: sdosanjh
4 Replies

10. UNIX for Dummies Questions & Answers

Changing directory through script

Hi Friends ! I want to change my current directory through a script. I am running the script say, from /home/proj directory. The script is like this : #!/usr/bin/sh cd module/pack/data once i run the script, i am still in /home/proj directory only. My problem is, i dont want to type... (5 Replies)
Discussion started by: mrgubbala
5 Replies

Featured Tech Videos