appendArgTar(){
# Desc: Writes first argument to temporary file with arguments as options, then appends file to tar
# Usage: appendArgTar "$(echo "Data to be written.")" [name of file to be inserted] [tar path] [temp dir] ([cmd1] [cmd2] [cmd3] [cmd4]...)
- # Version: 1.0.4
+ # Version: 1.0.6
# Input: arg1: data to be written
# arg2: file name of file to be inserted into tar
# arg3: tar archive path (must exist first)
# appendArgTar "$(cat /tmp/largefile1.gpg)" "largefile1" $HOME/archive.tar /tmp "gpg --decrypt" &
# appendArgTar "$(cat /tmp/largefile2.gpg)" "largefile2" $HOME/archive.tar /tmp "gpg --decrypt" &
# appendArgTar "$(cat /tmp/largefile3.gpg)" "largefile3" $HOME/archive.tar /tmp "gpg --decrypt" &
- # Depends: bash 5
+ # Depends: bash 5, tar 1, yell()
# Ref/Attrib: Using 'eval' to construct command strings https://askubuntu.com/a/476533
+ local fn fileName tarPath tmpDir cmd0 cmd1 cmd2 cmd3 cmd4
+
# Save function name
- local FN="${FUNCNAME[0]}";
- #yell "DEBUG:STATUS:$FN:Finished appendArgTar()."
+ fn="${FUNCNAME[0]}";
+ #yell "DEBUG:STATUS:$fn:Finished appendArgTar()."
# Set file name
- if ! [ -z "$2" ]; then FILENAME="$2"; else yell "ERROR:$FN:Not enough arguments."; exit 1; fi
+ if ! [ -z "$2" ]; then fileName="$2"; else yell "ERROR:$fn:Not enough arguments."; exit 1; fi
# Check tar path is a file
- if [ -f "$3" ]; then TAR_PATH="$3"; else yell "ERROR:$FN:Tar archive arg not a file."; exit 1; fi
+ if [ -f "$3" ]; then tarPath="$3"; else yell "ERROR:$fn:Tar archive arg not a file."; exit 1; fi
# Check temp dir arg
- if ! [ -z "$4" ]; then TMP_DIR="$4"; else yell "ERROR:$FN:No temporary working dir set."; exit 1; fi
+ if ! [ -z "$4" ]; then tmpDir="$4"; else yell "ERROR:$fn:No temporary working dir set."; exit 1; fi
# Set command strings
- if ! [ -z "$5" ]; then CMD1="$5"; else CMD1="tee /dev/null "; fi # command string 1
- if ! [ -z "$6" ]; then CMD2="$6"; else CMD2="tee /dev/null "; fi # command string 2
- if ! [ -z "$7" ]; then CMD3="$7"; else CMD3="tee /dev/null "; fi # command string 3
- if ! [ -z "$8" ]; then CMD4="$8"; else CMD4="tee /dev/null "; fi # command string 4
+ if ! [ -z "$5" ]; then cmd1="$5"; else cmd1="cat "; fi # command string 1
+ if ! [ -z "$6" ]; then cmd2="$6"; else cmd2="cat "; fi # command string 2
+ if ! [ -z "$7" ]; then cmd3="$7"; else cmd3="cat "; fi # command string 3
+ if ! [ -z "$8" ]; then cmd4="$8"; else cmd4="cat "; fi # command string 4
# Input command
- CMD0="echo \"\$1\""
+ cmd0="echo \"\$1\""
# # Debug
- # yell "DEBUG:STATUS:$FN:CMD0:$CMD0"
- # yell "DEBUG:STATUS:$FN:CMD1:$CMD1"
- # yell "DEBUG:STATUS:$FN:CMD2:$CMD2"
- # yell "DEBUG:STATUS:$FN:CMD3:$CMD3"
- # yell "DEBUG:STATUS:$FN:CMD4:$CMD4"
- # yell "DEBUG:STATUS:$FN:FILENAME:$FILENAME"
- # yell "DEBUG:STATUS:$FN:TAR_PATH:$TAR_PATH"
- # yell "DEBUG:STATUS:$FN:TMP_DIR:$TMP_DIR"
+ # yell "DEBUG:STATUS:$fn:cmd0:$cmd0"
+ # yell "DEBUG:STATUS:$fn:cmd1:$cmd1"
+ # yell "DEBUG:STATUS:$fn:cmd2:$cmd2"
+ # yell "DEBUG:STATUS:$fn:cmd3:$cmd3"
+ # yell "DEBUG:STATUS:$fn:cmd4:$cmd4"
+ # yell "DEBUG:STATUS:$fn:fileName:$fileName"
+ # yell "DEBUG:STATUS:$fn:tarPath:$tarPath"
+ # yell "DEBUG:STATUS:$fn:tmpDir:$tmpDir"
# Write to temporary working dir
- eval "$CMD0 | $CMD1 | $CMD2 | $CMD3 | $CMD4" > "$TMP_DIR"/"$FILENAME";
+ eval "$cmd0 | $cmd1 | $cmd2 | $cmd3 | $cmd4" > "$tmpDir"/"$fileName";
# Append to tar
- try tar --append --directory="$TMP_DIR" --file="$TAR_PATH" "$FILENAME";
- #yell "DEBUG:STATUS:$FN:Finished appendArgTar()."
+ try tar --append --directory="$tmpDir" --file="$tarPath" "$fileName";
+ #yell "DEBUG:STATUS:$fn:Finished appendArgTar()."
} # Append Bash var to file appended to Tar archive
#==BEGIN sample code==
myTar="/tmp/$(date +%s).tar";
+myTmpDir="/dev/shm"
try tar --create --file="$myTar" --files-from=/dev/null ;
myVar="how are you doing?"
-appendArgTar "$myVar" "$(date +%s)..LOUD_insidetar" "$myTar" "/dev/shm" "tee /tmp/$(date +%s)..outsidetar " "tr '[:lower:]' '[:upper:]'"
+appendArgTar "$myVar" "$(date +%s)..LOUD_insidetar" "$myTar" "$myTmpDir" "tee /tmp/$(date +%s)..outsidetar " "tr '[:lower:]' '[:upper:]'" && \
+ echo "Created $myTar using $myTmpDir as temp working dir."
#==END sample code==
# Author: Steven Baltakatei Sandoval
appendFileTar(){
# Desc: Processes first file and then appends to tar
# Usage: appendFileTar [file path] [name of file to be inserted] [tar path] [temp dir] ([cmd1] [cmd2] [cmd3] [cmd4]...)
- # Version: 1.0.2
+ # Version: 1.0.3
# Input: arg1: path of file to be (processed and) written
# arg2: name to use for file inserted into tar
# arg3: tar archive path (must exist first)
# appendFileTar /tmp/largefile1.gpg "largefile1" $HOME/archive.tar /tmp "gpg --decrypt" &
# appendFileTar /tmp/largefile2.gpg "largefile2" $HOME/archive.tar /tmp "gpg --decrypt" &
# appendFileTar /tmp/largefile3.gpg "largefile3" $HOME/archive.tar /tmp "gpg --decrypt" &
- # Depends: bash 5
+ # Depends: bash 5, tar 1, yell()
+
+ local fn fileName tarPath tmpDir
# Save function name
- local FN="${FUNCNAME[0]}";
- #yell "DEBUG:STATUS:$FN:Finished appendFileTar()."
+ fn="${FUNCNAME[0]}";
+ #yell "DEBUG:STATUS:$fn:Finished appendFileTar()."
# Set file name
- if ! [ -z "$2" ]; then FILENAME="$2"; else yell "ERROR:$FN:Not enough arguments."; exit 1; fi
+ if ! [ -z "$2" ]; then fileName="$2"; else yell "ERROR:$fn:Not enough arguments."; exit 1; fi
# Check tar path is a file
- if [ -f "$3" ]; then TAR_PATH="$3"; else yell "ERROR:$FN:Tar archive arg not a file."; exit 1; fi
+ if [ -f "$3" ]; then tarPath="$3"; else yell "ERROR:$fn:Tar archive arg not a file:$3"; exit 1; fi
# Check temp dir arg
- if ! [ -z "$4" ]; then TMP_DIR="$4"; else yell "ERROR:$FN:No temporary working dir set."; exit 1; fi
+ if ! [ -z "$4" ]; then tmpDir="$4"; else yell "ERROR:$fn:No temporary working dir set."; exit 1; fi
# Set command strings
- if ! [ -z "$5" ]; then CMD1="$5"; else CMD1="tee /dev/null "; fi # command string 1
- if ! [ -z "$6" ]; then CMD2="$6"; else CMD2="tee /dev/null "; fi # command string 2
- if ! [ -z "$7" ]; then CMD3="$7"; else CMD3="tee /dev/null "; fi # command string 3
- if ! [ -z "$8" ]; then CMD4="$8"; else CMD4="tee /dev/null "; fi # command string 4
+ if ! [ -z "$5" ]; then cmd1="$5"; else cmd1="cat "; fi # command string 1
+ if ! [ -z "$6" ]; then cmd2="$6"; else cmd2="cat "; fi # command string 2
+ if ! [ -z "$7" ]; then cmd3="$7"; else cmd3="cat "; fi # command string 3
+ if ! [ -z "$8" ]; then cmd4="$8"; else cmd4="cat "; fi # command string 4
# Input command string
- CMD0="cat \"\$1\""
+ cmd0="cat \"\$1\""
# # Debug
- # yell "DEBUG:STATUS:$FN:CMD0:$CMD0"
- # yell "DEBUG:STATUS:$FN:CMD1:$CMD1"
- # yell "DEBUG:STATUS:$FN:CMD2:$CMD2"
- # yell "DEBUG:STATUS:$FN:CMD3:$CMD3"
- # yell "DEBUG:STATUS:$FN:CMD4:$CMD4"
- # yell "DEBUG:STATUS:$FN:FILENAME:$FILENAME"
- # yell "DEBUG:STATUS:$FN:TAR_PATH:$TAR_PATH"
- # yell "DEBUG:STATUS:$FN:TMP_DIR:$TMP_DIR"
+ # yell "DEBUG:STATUS:$fn:cmd0:$cmd0"
+ # yell "DEBUG:STATUS:$fn:cmd1:$cmd1"
+ # yell "DEBUG:STATUS:$fn:cmd2:$cmd2"
+ # yell "DEBUG:STATUS:$fn:cmd3:$cmd3"
+ # yell "DEBUG:STATUS:$fn:cmd4:$cmd4"
+ # yell "DEBUG:STATUS:$fn:fileName:$fileName"
+ # yell "DEBUG:STATUS:$fn:tarPath:$tarPath"
+ # yell "DEBUG:STATUS:$fn:tmpDir:$tmpDir"
# Write to temporary working dir
- eval "$CMD0 | $CMD1 | $CMD2 | $CMD3 | $CMD4" > "$TMP_DIR"/"$FILENAME";
+ eval "$cmd0 | $cmd1 | $cmd2 | $cmd3 | $cmd4" > "$tmpDir"/"$fileName";
# Append to tar
- try tar --append --directory="$TMP_DIR" --file="$TAR_PATH" "$FILENAME";
- #yell "DEBUG:STATUS:$FN:Finished appendFileTar()."
+ try tar --append --directory="$tmpDir" --file="$tarPath" "$fileName";
+ #yell "DEBUG:STATUS:$fn:Finished appendFileTar()."
} # Append file to Tar archive
#==BEGIN sample code==
myTar="/tmp/$(date +%s).tar";
+myTmpDir="/dev/shm";
try tar --create --file="$myTar" --files-from=/dev/null ;
-myFile="/tmp/$(date +%s)..original"
-echo "how are you doing?" > "$myFile"
-appendFileTar "$myFile" "$(date +%s)..LOUD_insidetar" "$myTar" "/dev/shm" "tee /tmp/$(date +%s)..outsidetar " "tr '[:lower:]' '[:upper:]'"
+myFile="/tmp/$(date +%s)..original";
+echo "how are you doing?" > "$myFile";
+appendFileTar "$myFile" "$(date +%s)..LOUD_insidetar" "$myTar" "$myTmpDir" "tee /tmp/$(date +%s)..outsidetar " "tr '[:lower:]' '[:upper:]'" && \
+ echo "Inserted a processed version of $myFile into $myTar using temp dir $myTmpDir ";
#==END sample code==
# Author: Steven Baltakatei Sandoval
#!/bin/bash
-
# Desc: Template to check that apps, files, or dirs exist.
-# Author: Steven Baltaktei Sandoval
-# License: GPLv3+
#==BEGIN Define script parameters==
declare -Ag appRollCall # Associative array for storing app status
declare -Ag fileRollCall # Associative array for storing file status
declare -Ag dirRollCall # Associative array for storing dir status
+#==END Define script parameters==
#===BEGIN Declare local script functions===
checkapp() {
# Desc: If arg is a command, save result in assoc array 'appRollCall'
# Usage: checkapp arg1 arg2 arg3 ...
+ # Version: 0.1.0
# Input: global assoc. array 'appRollCall'
# Output: adds/updates key(value) to global assoc array 'appRollCall'
local returnState
checkfile() {
# Desc: If arg is a file path, save result in assoc array 'fileRollCall'
# Usage: checkfile arg1 arg2 arg3 ...
+ # Version: 0.1.0
# Input: global assoc. array 'fileRollCall'
# Output: adds/updates key(value) to global assoc array 'fileRollCall';
# Output: returns 0 if app found, 1 otherwise
checkdir() {
# Desc: If arg is a dir path, save result in assoc array 'dirRollCall'
# Usage: checkdir arg1 arg2 arg3 ...
+ # Version 0.1.0
# Input: global assoc. array 'dirRollCall'
# Output: adds/updates key(value) to global assoc array 'dirRollCall';
# Output: returns 0 if app found, 1 otherwise
return 1;
fi
} # Check that dir exists
-#===END Declare local script functions===
-#==END Define script parameters==
-
-#==BEGIN sample code==
-if checkapp cat; then echo "cat found."; fi
-if checkapp gpg; then echo "gpg found."; fi
-if checkapp emaaaacs; then echo "emaaaacs found."; fi
-#==END sample code==
-
-#==BEGIN Display errors==
-#===BEGIN Display Missing Apps===
-missingApps="Missing apps :"
-#for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
-for key in "${!appRollCall[@]}"; do
- value="${appRollCall[$key]}"
- if [ "$value" = "false" ]; then
- #echo "DEBUG:Missing apps: $key => $value";
- missingApps="$missingApps""$key "
- appMissing="true"
+displayMissing() {
+ # Desc: Displays missing apps, files, and dirs
+ # Usage: displayMissing
+ # Version 0.1.0
+ # Input: associative arrays: appRollCall, fileRollCall, dirRollCall
+ # Output: stderr: messages indicating missing apps, file, or dirs
+ # Depends: bash 5, checkAppFileDir()
+ #==BEGIN Display errors==
+ #===BEGIN Display Missing Apps===
+ missingApps="Missing apps :"
+ #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
+ for key in "${!appRollCall[@]}"; do
+ value="${appRollCall[$key]}"
+ if [ "$value" = "false" ]; then
+ #echo "DEBUG:Missing apps: $key => $value";
+ missingApps="$missingApps""$key "
+ appMissing="true"
+ fi
+ done
+ if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing.
+ echo "$missingApps" 1>&2;
fi
-done
-if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing.
- echo "$missingApps" 1>&2;
-fi
-#===END Display Missing Apps===
+ #===END Display Missing Apps===
-#===BEGIN Display Missing Files===
-missingFiles="Missing files:"
-#for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done
-for key in "${!fileRollCall[@]}"; do
- value="${fileRollCall[$key]}"
- if [ "$value" = "false" ]; then
- #echo "DEBUG:Missing files: $key => $value";
- missingFiles="$missingFiles""$key "
- fileMissing="true"
+ #===BEGIN Display Missing Files===
+ missingFiles="Missing files:"
+ #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done
+ for key in "${!fileRollCall[@]}"; do
+ value="${fileRollCall[$key]}"
+ if [ "$value" = "false" ]; then
+ #echo "DEBUG:Missing files: $key => $value";
+ missingFiles="$missingFiles""$key "
+ fileMissing="true"
+ fi
+ done
+ if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing.
+ echo "$missingFiles" 1>&2;
fi
-done
-if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing.
- echo "$missingFiles" 1>&2;
-fi
-#===END Display Missing Files===
+ #===END Display Missing Files===
-#===BEGIN Display Missing Directories===
-missingDirs="Missing dirs:"
-#for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done
-for key in "${!dirRollCall[@]}"; do
- value="${dirRollCall[$key]}"
- if [ "$value" = "false" ]; then
- #echo "DEBUG:Missing dirs: $key => $value";
- missingDirs="$missingDirs""$key "
- dirMissing="true"
+ #===BEGIN Display Missing Directories===
+ missingDirs="Missing dirs:"
+ #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done
+ for key in "${!dirRollCall[@]}"; do
+ value="${dirRollCall[$key]}"
+ if [ "$value" = "false" ]; then
+ #echo "DEBUG:Missing dirs: $key => $value";
+ missingDirs="$missingDirs""$key "
+ dirMissing="true"
+ fi
+ done
+ if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing.
+ echo "$missingDirs" 1>&2;
fi
-done
-if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing.
- echo "$missingDirs" 1>&2;
-fi
-#===END Display Missing Directories===
+ #===END Display Missing Directories===
+
+ #==END Display errors==
+} # Display missing apps, files, dirs
+
+#===END Declare local script functions===
+#==END Define script parameters==
-#===BEGIN Display Failed Functions===
-if [ ${#failedFunctions} -gt 0 ]; then # Only indicate if an function failed.
- echo "Failed functions:${failedFunctions[@]}" 1>&2;
-fi
-#===END Display Failed Functions===
+#==BEGIN sample code==
+if checkapp cat; then echo "cat found."; fi;
+if checkapp gpg; then echo "gpg found."; fi;
+if checkapp emaaaacs; then echo "emaaaacs found."; fi;
+sleep 1;
+displayMissing;
+#==END sample code==
-#==END Display errors==
+# Author: Steven Baltaktei Sandoval
+# License: GPLv3+
#!/bin/bash
-
# Desc: Checks that a valid tar archive exists, creates one otherwise
#===BEGIN Declare local script functions===
checkMakeTar() {
# Desc: Checks that a valid tar archive exists, creates one otherwise
# Usage: checkMakeTar [ path ]
- # Version: 1.0.1
+ # Version: 1.0.2
# Input: arg1: path of tar archive
# Output: exit code 0 : tar readable
# exit code 1 : tar missing; created
# exit code 2 : tar not readable; moved; replaced
- # Depends: try, tar, date
- local PATH_TAR returnFlag0 returnFlag1 returnFlag2
- PATH_TAR="$1"
+ # Depends: bash 5, date 8, tar 1, try()
+ local pathTar returnFlag0 returnFlag1 returnFlag2
+ pathTar="$1";
# Check if file is a valid tar archive
- if tar --list --file="$PATH_TAR" 1>/dev/null 2>&1; then
+ if tar --list --file="$pathTar" 1>/dev/null 2>&1; then
## T1: return success
returnFlag0="tar valid";
else
## F1: Check if file exists
- if [[ -f "$PATH_TAR" ]]; then
+ if [[ -f "$pathTar" ]]; then
### T: Rename file
- try mv "$PATH_TAR" "$PATH_TAR""--broken--""$(date +%Y%m%dT%H%M%S)" && \
+ try mv "$pathTar" "$pathTar""--broken--""$(date +%Y%m%dT%H%M%S)" && \
returnFlag1="tar moved";
else
### F: -
:
- fi
+ fi;
## F2: Create tar archive, return 0
- try tar --create --file="$PATH_TAR" --files-from=/dev/null && \
+ try tar --create --file="$pathTar" --files-from=/dev/null && \
returnFlag2="tar created";
- fi
+ fi;
# Determine function return code
if [[ "$returnFlag0" = "tar valid" ]]; then
return 1; # tar missing so created
elif [[ "$returnFlag2" = "tar created" ]] && [[ "$returnFlag1" = "tar moved" ]]; then
return 2; # tar not readable so moved; replaced
- fi
+ fi;
} # checks if arg1 is tar; creates one otherwise
#===END Declare local script functions===
dateShort(){
# Desc: Date without separators (YYYYmmdd)
# Usage: dateShort ([str date])
- # Version: 1.1.0
+ # Version: 1.1.1
# Input: arg1: 'date'-parsable timestamp string (optional)
# Output: stdout: date (ISO-8601, no separators)
# Depends: yell
- local TIME_CURRENT DATE_CURRENT_SHORT
+ local argTime timeCurrent timeInput dateCurrentShort
argTime="$1";
# Get Current Time
- TIME_CURRENT="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second.
+ timeCurrent="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second.
# Decide to parse current or supplied date
## Check if time argument empty
if [[ -z "$argTime" ]]; then
## T: Time argument empty, use current time
- TIME_INPUT="$TIME_CURRENT";
+ timeInput="$timeCurrent";
else
## F: Time argument exists, validate time
if date --date="$argTime" 1>/dev/null 2>&1; then
### T: Time argument is valid; use it
- TIME_INPUT="$argTime";
+ timeInput="$argTime";
else
### F: Time argument not valid; exit
yell "ERROR:Invalid time argument supplied. Exiting."; exit 1;
fi
fi
# Construct and deliver separator-les date string
- DATE_CURRENT_SHORT="$(date -d "$TIME_INPUT" +%Y%m%d)"; # Produce separator-less current date with resolution 1 day.
- echo "$DATE_CURRENT_SHORT";
+ dateCurrentShort="$(date -d "$timeInput" +%Y%m%d)"; # Produce separator-less current date with resolution 1 day.
+ echo "$dateCurrentShort";
} # Get YYYYmmdd
#==BEGIN sample code==
dateTimeShort(){
# Desc: Timestamp without separators (YYYYmmddTHHMMSS+zzzz)
# Usage: dateTimeShort ([str date])
- # Version 1.1.0
+ # Version 1.1.1
# Input: arg1: 'date'-parsable timestamp string (optional)
# Output: stdout: timestamp (ISO-8601, no separators)
# Depends: yell
- local TIME_CURRENT TIME_CURRENT_SHORT
+ local argTime timeCurrent timeInput timeCurrentShort
argTime="$1";
# Get Current Time
- TIME_CURRENT="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second.
+ timeCurrent="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second.
# Decide to parse current or supplied date
## Check if time argument empty
if [[ -z "$argTime" ]]; then
## T: Time argument empty, use current time
- TIME_INPUT="$TIME_CURRENT";
+ timeInput="$timeCurrent";
else
## F: Time argument exists, validate time
if date --date="$argTime" 1>/dev/null 2>&1; then
### T: Time argument is valid; use it
- TIME_INPUT="$argTime";
+ timeInput="$argTime";
else
### F: Time argument not valid; exit
yell "ERROR:Invalid time argument supplied. Exiting."; exit 1;
fi
fi
# Construct and deliver separator-les date string
- TIME_CURRENT_SHORT="$(date -d "$TIME_INPUT" +%Y%m%dT%H%M%S%z)";
- echo "$TIME_CURRENT_SHORT";
+ timeCurrentShort="$(date -d "$timeInput" +%Y%m%dT%H%M%S%z)";
+ echo "$timeCurrentShort";
} # Get YYYYmmddTHHMMSS±zzzz
#==BEGIN sample code==
setTimeZoneEV(){
# Desc: Set time zone environment variable TZ
# Usage: setTimeZoneEV arg1
+ # Version 0.1.1
# Input: arg1: 'date'-compatible timezone string (ex: "America/New_York")
# TZDIR env var (optional; default: "/usr/share/zoneinfo")
# Output: exports TZ
# exit code 2 if unable to validate arg1
# Depends: yell, printenv, bash 5
# Tested on: Debian 10
- ARG1="$1"
- local tzDir returnState
+ local tzDir returnState argTimeZone
+
+ argTimeZone="$1"
if ! [[ $# -eq 1 ]]; then
yell "ERROR:Invalid argument count.";
return 1;
fi
# Validate TZ string
- if ! [[ -f "$tzDir"/"$ARG1" ]]; then
+ if ! [[ -f "$tzDir"/"$argTimeZone" ]]; then
yell "ERROR:Invalid time zone argument.";
return 2;
else
# Export ARG1 as TZ environment variable
- TZ="$ARG1" && export TZ && returnState="true";
+ TZ="$argTimeZone" && export TZ && returnState="true";
fi
# Determine function return code
# Ref/Attrib: ISO-8601:2004(E), §4.4.4.2 Representations of time intervals by duration and context information
# Note: "1 month" ("P1M") is assumed to be "30 days" (see ISO-8601:2004(E), §2.2.1.2)
# Usage: timeDuration [1:seconds] ([2:precision])
- # Version: 1.0.3
+ # Version: 1.0.4
# Input: arg1: seconds as base 10 integer >= 0 (ex: 3601)
# arg2: precision level (optional; default=2)
# Output: stdout: ISO-8601 duration string (ex: "P1H1S", "P2Y10M15DT10H30M20S")
# exit code 1: error_input
# exit code 2: error_unknown
# Example: 'timeDuration 111111 3' yields 'P1DT6H51M'
- # Depends: date 8 (gnucoreutils), yell,
- local returnState argSeconds argPrecision remainder precision witherPrecision
+ # Depends: date 8, bash 5, yell,
+ local argSeconds argPrecision precision returnState remainder
local fullYears fullMonths fullDays fullHours fullMinutes fullSeconds
- local displayYears displayMonths displayDays displayHours displayMinutes displaySeconds
local hasYears hasMonths hasDays hasHours hasMinutes hasSeconds
+ local witherPrecision output
+ local displayYears displayMonths displayDays displayHours displayMinutes displaySeconds
argSeconds="$1"; # read arg1 (seconds)
argPrecision="$2"; # read arg2 (precision)
displayDateTime="true"; else displayDateTime="false"; fi
## Construct duration output string
- OUTPUT="P"
+ output="P"
if $displayYears; then
- OUTPUT=$OUTPUT$fullYears"Y"; fi
+ output=$output$fullYears"Y"; fi
if $displayMonths; then
- OUTPUT=$OUTPUT$fullMonths"M"; fi
+ output=$output$fullMonths"M"; fi
if $displayDays; then
- OUTPUT=$OUTPUT$fullDays"D"; fi
+ output=$output$fullDays"D"; fi
if $displayDateTime; then
- OUTPUT=$OUTPUT"T"; fi
+ output=$output"T"; fi
if $displayHours; then
- OUTPUT=$OUTPUT$fullHours"H"; fi
+ output=$output$fullHours"H"; fi
if $displayMinutes; then
- OUTPUT=$OUTPUT$fullMinutes"M"; fi
+ output=$output$fullMinutes"M"; fi
if $displaySeconds; then
- OUTPUT=$OUTPUT$fullSeconds"S"; fi
+ output=$output$fullSeconds"S"; fi
## Output duration string to stdout
- echo "$OUTPUT" && returnState="true";
+ echo "$output" && returnState="true";
#===Determine function return code===
if [ "$returnState" = "true" ]; then
echo "Precision 2 duration:$(timeDuration "$(date +%s)" 2)"
echo "Precision 1 duration:$(timeDuration "$(date +%s)" 1)"
echo "Precision 6 duration:$(timeDuration $((60+60*60+60*60*24+60*60*24*30+60*60*24*365 - (60+60*60+60*60*24+60*60*24*30) )) 6)"
-timeDuration "$@"
+echo "Parsing argument provided to script:\"$*\""; timeDuration "$@"
#==END sample code==
# Author: Steven Baltakatei Sandoval
timeEpochNS() {
# Desc: Get epoch nanoseconds
# Usage: timeEpochNS
- # Version 0.2.2
+ # Version 0.2.3
# Input: arg1: 'date'-parsable timestamp string (optional)
# Output: Nanoseconds since 1970-01-01
- # Depends: date 8, yell()
+ # Depends: date 8, cut 8, yell()
# Ref/Attrib: Force base 10 Bash arith with '10#'. https://stackoverflow.com/a/24777667
- local TIME_CURRENT TIME_INPUT TIME_EPOCH_FLOAT TIME_EPOCH_NSFRAC
- local TIME_EPOCH_NS
+ local argTime timeCurrent timeInput timeEpochFloat timeEpochInt
+ local timeEpochNsFrac timeEpochNs
argTime="$1";
# Get Current Time
- TIME_CURRENT="$(date --iso-8601=ns)"; # Produce `date`-parsable current timestamp with resolution of 1 nanosecond.
+ timeCurrent="$(date --iso-8601=ns)"; # Produce `date`-parsable current timestamp with resolution of 1 nanosecond.
# Decide to parse current or supplied time
## Check if time argument empty
if [[ -z "$argTime" ]]; then
## T: Time argument empty, use current time
- TIME_INPUT="$TIME_CURRENT";
+ timeInput="$timeCurrent";
else
## F: Time argument exists, validate time
if date --date="$argTime" 1>/dev/null 2>&1; then
### T: Time argument is valid; use it
- TIME_INPUT="$argTime";
+ timeInput="$argTime";
else
### F: Time argument not valid; exit
yell "ERROR:Invalid time argument supplied. Exiting."; exit 1;
fi;
fi;
# Construct and deliver nanoseconds since 1970-01-01
- TIME_EPOCH_FLOAT="$(date --date="$TIME_INPUT" +%s.%N)"; # Save ssss.NNNNNNNNN
- TIME_EPOCH_INT="$(echo "$TIME_EPOCH_FLOAT" | cut -d. -f1)"; # Get ssss
- TIME_EPOCH_NSFRAC="$(echo "$TIME_EPOCH_FLOAT" | cut -d. -f2)"; # Get NNNNNNNNN
- TIME_EPOCH_NS="$(( (10#"$TIME_EPOCH_INT" * 10**9) + (10#"$TIME_EPOCH_NSFRAC") ))";
- echo "$TIME_EPOCH_NS";
+ timeEpochFloat="$(date --date="$timeInput" +%s.%N)"; # Save ssss.NNNNNNNNN
+ timeEpochInt="$(echo "$timeEpochFloat" | cut -d. -f1)"; # Get ssss
+ timeEpochNsFrac="$(echo "$timeEpochFloat" | cut -d. -f2)"; # Get NNNNNNNNN
+ timeEpochNs="$(( (10#"$timeEpochInt" * 10**9) + (10#"$timeEpochNsFrac") ))";
+ echo "$timeEpochNs";
} # Nanoseconds since 1970-01-01
#==BEGIN sample code==
try() { "$@" || die "cannot $*"; }
timeUntilNextDay(){
# Desc: Report seconds until next day.
- # Version: 1.0.0
+ # Version: 1.0.3
# Output: stdout: integer seconds until next day
# Output: exit code 0 if stdout > 0; 1 if stdout = 0; 2 if stdout < 0
# Usage: timeUntilNextDay
# Usage: if ! myTTL="$(timeUntilNextDay)"; then yell "ERROR in if statement"; exit 1; fi
# Depends: date 8, echo 8, yell, try
- local returnState TIME_CURRENT TIME_NEXT_DAY SECONDS_UNTIL_NEXT_DAY
-
- TIME_CURRENT="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second.
- TIME_NEXT_DAY="$(date -d "$TIME_CURRENT next day" --iso-8601=date)"; # Produce timestamp of beginning of tomorrow with resolution of 1 second.
- SECONDS_UNTIL_NEXT_DAY="$(( $(date +%s -d "$TIME_NEXT_DAY") - $(date +%s -d "$TIME_CURRENT") ))" ; # Calculate seconds until closest future midnight (res. 1 second).
- if [[ "$SECONDS_UNTIL_NEXT_DAY" -gt 0 ]]; then
+ local returnState timeCurrent timeNextDay secondsUntilNextDay
+ timeCurrent="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second.
+ timeNextDay="$(date -d "$timeCurrent next day" --iso-8601=date)"; # Produce timestamp of beginning of tomorrow with resolution of 1 second.
+ secondsUntilNextDay="$(( $(date +%s -d "$timeNextDay") - $(date +%s -d "$timeCurrent") ))" ; # Calculate seconds until closest future midnight (res. 1 second).
+ if [[ "$secondsUntilNextDay" -gt 0 ]]; then
returnState="true";
- elif [[ "$SECONDS_UNTIL_NEXT_DAY" -eq 0 ]]; then
+ elif [[ "$secondsUntilNextDay" -eq 0 ]]; then
returnState="warning_zero";
yell "WARNING:Reported time until next day exactly zero.";
- elif [[ "$SECONDS_UNTIL_NEXT_DAY" -lt 0 ]]; then
+ elif [[ "$secondsUntilNextDay" -lt 0 ]]; then
returnState="warning_negative";
yell "WARNING:Reported time until next day is negative.";
fi
- try echo "$SECONDS_UNTIL_NEXT_DAY"; # Report
+ try echo "$secondsUntilNextDay"; # Report
# Determine function return code
if [[ "$returnState" = "true" ]]; then
try() { "$@" || die "cannot $*"; }
timeUntilNextHour(){
# Desc: Report seconds until next hour
- # Version 1.0.0
+ # Version 1.0.1
# Output: stdout: integer seconds until next hour
# Output: exit code 0 if stdout > 0; 1 if stdout = 0; 2 if stdout < 0
# Usage: timeUntilNextHour
# Usage: if ! myTTL="$(timeUntilNextHour)"; then yell "ERROR in if statement"; exit 1; fi
- local returnState TIME_CURRENT TIME_NEXT_HOUR SECONDS_UNTIL_NEXT_HOUR
- TIME_CURRENT="$(date --iso-8601=seconds)"; # Produce `date`-parsable current timestamp with resolution of 1 second.
- TIME_NEXT_HOUR="$(date -d "$TIME_CURRENT next hour" --iso-8601=hours)"; # Produce `date`-parsable current time stamp with resolution of 1 second.
- SECONDS_UNTIL_NEXT_HOUR="$(( $(date +%s -d "$TIME_NEXT_HOUR") - $(date +%s -d "$TIME_CURRENT") ))"; # Calculate seconds until next hour (res. 1 second).
- if [[ "$SECONDS_UNTIL_NEXT_HOUR" -gt 0 ]]; then
+ local returnState timeCurrent timeNextHour secondsUntilNextHour
+ timeCurrent="$(date --iso-8601=seconds)"; # Produce `date`-parsable current timestamp with resolution of 1 second.
+ timeNextHour="$(date -d "$timeCurrent next hour" --iso-8601=hours)"; # Produce `date`-parsable current time stamp with resolution of 1 second.
+ secondsUntilNextHour="$(( $(date +%s -d "$timeNextHour") - $(date +%s -d "$timeCurrent") ))"; # Calculate seconds until next hour (res. 1 second).
+ if [[ "$secondsUntilNextHour" -gt 0 ]]; then
returnState="true";
- elif [[ "$SECONDS_UNTIL_NEXT_HOUR" -eq 0 ]]; then
+ elif [[ "$secondsUntilNextHour" -eq 0 ]]; then
returnState="warning_zero";
yell "WARNING:Reported time until next hour exactly zero.";
- elif [[ "$SECONDS_UNTIL_NEXT_HOUR" -lt 0 ]]; then
+ elif [[ "$secondsUntilNextHour" -lt 0 ]]; then
returnState="warning_negative";
yell "WARNING:Reported time until next hour is negative.";
- fi
+ fi;
- try echo "$SECONDS_UNTIL_NEXT_HOUR"; # Report
+ try echo "$secondsUntilNextHour"; # Report
# Determine function return code
if [[ "$returnState" = "true" ]]; then
return 1;
elif [[ "$returnState" = "warning_negative" ]]; then
return 2;
- fi
+ fi;
} # Report seconds until next hour
#===END Declare local script functions===
#==END Define script parameters==
validateInput() {
# Desc: Validates Input
# Usage: validateInput [str input] [str input type]
- # Version: 0.3.0
+ # Version: 0.3.1
# Input: arg1: string to validate
# arg2: string specifying input type (ex:"ssh_pubkey")
# Output: return code 0: if input string matched specified string type
- # Depends: bash 5, yell
+ # Depends: bash 5, yell()
+
+ local fn argInput argType
# Save function name
- local FN="${FUNCNAME[0]}";
+ fn="${FUNCNAME[0]}";
# Process arguments
argInput="$1";
argType="$2";
- if [[ $# -gt 2 ]]; then yell "ERROR:$0:$FN:Too many arguments."; exit 1; fi;
+ if [[ $# -gt 2 ]]; then yell "ERROR:$0:$fn:Too many arguments."; exit 1; fi;
# Check for blank
if [[ -z "$argInput" ]]; then return 1; fi
#==BEGIN sample code==
testKey="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCq8G0XWi8A+MXNu7MEaxDsZJUwtgPC4shGAAarXC4PQTRxLfUHtyWkNbzGh31U/bKI3Snp1O78yOVEZSjdmWkDvkwtWKJQcoZBStgqw9oIPE0oG8uh49XoX77nVabi0JCf8zO63Ai4/9EdwyqE9xsJ+soOYt2V5+UCj0zsv1xpt2YhfYyXrzBv9x6k9lCxy0NW3Ik1mSW+OzSOT3tgDf36ujV/CI2i8ERM9civWy8FtFOReZMV2kkbj7fXI4E1wKM1RFIn7er1MseCSvbvh3o2uCO46/euqHAstYs5cL+4yB2qM/xKfHT5aocDjq7GBLKtree9xdNF0EE9CNr/+J5R";
keyType="ssh_pubkey";
-if validateInput "$testKey" "$keyType"; then echo "Looks like a valid $keyType:\"$testKey\""; else echo "doesn't look like a valid $keyType:\"$testKey\""; fi
+if validateInput "$testKey" "$keyType"; then echo "Looks like a valid $keyType:\"$testKey\""; else echo "Doesn't look like a valid $keyType:\"$testKey\""; fi
echo "";
testKey="blah-blah onetwothreeREEE";
keyType="ssh_pubkey";
-if validateInput "$testKey" "$keyType"; then echo "Looks like a valid $keyType:\"$testKey\""; else echo "doesn't look like a valid $keyType:\"$testKey\""; fi
+if validateInput "$testKey" "$keyType"; then echo "Looks like a valid $keyType:\"$testKey\""; else echo "Doesn't look like a valid $keyType:\"$testKey\""; fi
echo "";
-testKey3="";
+testKey="";
keyType="ssh_pubkey";
-if validateInput "$testKey" "$keyType"; then echo "Looks like a valid $keyType:\"$testKey\""; else echo "doesn't look like a valid $keyType:\"$testKey\""; fi
+if validateInput "$testKey" "$keyType"; then echo "Looks like a valid $keyType:\"$testKey\""; else echo "Doesn't look like a valid $keyType:\"$testKey\""; fi
echo "";
testKey="age1c3s6huz6en6jh40cem0erc0m2un2ry85my0t0ujj4a5cu8gzkysq9l39e4";
keyType="age_pubkey";
-if validateInput "$testKey" "$keyType"; then echo "Looks like a valid $keyType:\"$testKey\""; else echo "doesn't look like a valid $keyType:\"$testKey\""; fi
+if validateInput "$testKey" "$keyType"; then echo "Looks like a valid $keyType:\"$testKey\""; else echo "Doesn't look like a valid $keyType:\"$testKey\""; fi
echo "";
testKey="123"
keyType="integer";
-if validateInput "$testKey" "$keyType"; then echo "Looks like a valid $keyType:\"$testKey\""; else echo "doesn't look like a valid $keyType:\"$testKey\""; fi
+if validateInput "$testKey" "$keyType"; then echo "Looks like a valid $keyType:\"$testKey\""; else echo "Doesn't look like a valid $keyType:\"$testKey\""; fi
echo "";
testKey="123abc"
keyType="integer";
-if validateInput "$testKey" "$keyType"; then echo "Looks like a valid $keyType:\"$testKey\""; else echo "doesn't look like a valid $keyType:\"$testKey\""; fi
+if validateInput "$testKey" "$keyType"; then echo "Looks like a valid $keyType:\"$testKey\""; else echo "Doesn't look like a valid $keyType:\"$testKey\""; fi
echo "";
testKey="year"
keyType="time_element";
-if validateInput "$testKey" "$keyType"; then echo "Looks like a valid $keyType:\"$testKey\""; else echo "doesn't look like a valid $keyType:\"$testKey\""; fi
+if validateInput "$testKey" "$keyType"; then echo "Looks like a valid $keyType:\"$testKey\""; else echo "Doesn't look like a valid $keyType:\"$testKey\""; fi
echo "";
#==END sample code==
--- /dev/null
+#!/bin/bash
+# Desc: Display message if optionVerbose true
+
+vbm() {
+ # Description: Prints verbose message ("vbm") to stderr if optionVerbose is set to "true".
+ # Usage: vbm "DEBUG:verbose message here"
+ # Version 0.1.1
+ # Input: arg1: string
+ # vars: optionVerbose
+ # Output: stderr
+ # Depends: echo 8, date 8
+
+ if [ "$optionVerbose" = "true" ]; then
+ functionTime=$(date --iso-8601=ns); # Save current time in nano seconds.
+ echo "[$functionTime] ""$*" 1>&2; # Display argument text.
+ fi
+
+ # End function
+ return 0; # Function finished.
+} # Displays message if optionVerbose true
+
+# Author: Steven Baltakatei Sandoval
+# License: GPLv3+