fix(unitproc):appendFileTar: Use 'eval' to build cmd string
[BK-2020-03.git] / unitproc / bktemp-setTimeZoneEV
1 #!/bin/bash
2
3 # Desc: Set time zone environment variable TZ
4
5 yell() { echo "$0: $*" >&2; } # Yell, Die, Try Three-Fingered Claw technique; # Ref/Attrib: https://stackoverflow.com/a/25515370
6 die() { yell "$*"; exit 111; }
7 try() { "$@" || die "cannot $*"; }
8 setTimeZoneEV(){
9 # Desc: Set time zone environment variable TZ
10 # Usage: setTimeZoneEV arg1
11 # Input: arg1: 'date'-compatible timezone string (ex: "America/New_York")
12 # TZDIR env var (optional; default: "/usr/share/zoneinfo")
13 # Output: exports TZ
14 # exit code 0 on success
15 # exit code 1 on incorrect number of arguments
16 # exit code 2 if unable to validate arg1
17 # Depends: yell, printenv, bash 5
18 # Tested on: Debian 10
19 ARG1="$1"
20 local tzDir returnState
21 if ! [[ $# -eq 1 ]]; then
22 yell "ERROR:Invalid argument count.";
23 return 1;
24 fi
25
26 # Read TZDIR env var if available
27 if printenv TZDIR 1>/dev/null 2>&1; then
28 tzDir="$(printenv TZDIR)";
29 else
30 tzDir="/usr/share/zoneinfo";
31 fi
32
33 # Validate TZ string
34 if ! [[ -f "$tzDir"/"$ARG1" ]]; then
35 yell "ERROR:Invalid time zone argument.";
36 return 2;
37 else
38 # Export ARG1 as TZ environment variable
39 TZ="$ARG1" && export TZ && returnState="true";
40 fi
41
42 # Determine function return code
43 if [ "$returnState" = "true" ]; then
44 return 0;
45 fi
46 } # Exports TZ environment variable
47
48 #==BEGIN sample code==
49 date --iso-8601=seconds; echo "==============="; sleep 2
50
51 date --iso-8601=seconds
52 cmd1="setTimeZoneEV America/New_York"
53 echo "Running:$cmd1"; $cmd1; echo "Exit code:$?"
54 date --iso-8601=seconds; echo "==============="; sleep 2
55
56 date --iso-8601=seconds
57 cmd2="setTimeZoneEV Asia/Tokyo"
58 echo "Running:$cmd2"; $cmd2; echo "Exit code:$?"
59 date --iso-8601=seconds; echo "==============="; sleep 2
60
61 date --iso-8601=seconds
62 cmd3="setTimeZoneEV";
63 echo "Running:$cmd3"; $cmd3; echo "Exit code:$?"
64 date --iso-8601=seconds; echo "==============="; sleep 2
65
66 date --iso-8601=seconds
67 cmd4="setTimeZoneEV Pacific/Lemuria"
68 echo "Running:$cmd4"; $cmd4; echo "Exit code:$?"
69 date --iso-8601=seconds; echo "==============="; sleep 2
70
71 try setTimeZoneEV Atlantic/Atlantis
72 #==END sample code==
73
74 # Author: Steven Baltakatei Sandoval
75 # License: GPLv3+
76
77