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