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:
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?
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?
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).
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
}
$ settz
Enter timezone (Asia, or GMT) [GMT]:
$
$ echo $TZ
$
$
$ settz
Enter timezone (Asia, or GMT) [GMT]: Asia
$
$ echo $TZ
Asia/Kolkata
$
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:
TZ=UTC0 date;TZ=US/Pacific date;(unset TZ;date)
produce:
Sat Nov 23 17:16:34 UTC 2013
Sat Nov 23 09:16:34 PST 2013
Sat Nov 23 09:16:34 PST 2013