fix(bklog):Fix boolean '-' test in magicParseProcessStrings()
[EVA-2020-02.git] / exec / bklog
index f3722df9885a7dc0760c3842e6ae9df70e807764..52f6eb1ca116f37790b82adb721d8245cc0743b8 100644 (file)
@@ -11,7 +11,7 @@ dirTmpDefault="/dev/shm"; # Default parent of working directory
 
 # Script Metadata
 scriptName="bklog";             # Define basename of script file.
-scriptVersion="0.1.5";          # Define version of script.
+scriptVersion="0.1.8";          # Define version of script.
 scriptURL="https://gitlab.com/baltakatei/ninfacyzga-01"; # Define wesite hosting this script.
 scriptTimeStart="$(date +%Y%m%dT%H%M%S.%N)"; # YYYYmmddTHHMMSS.NNNNNNNNN
 scriptHostname=$(hostname);     # Save hostname of system running this script.
@@ -624,6 +624,60 @@ timeDuration(){
     fi
 
 } # Get duration (ex: PT10M4S )
+validateInput() {
+    # Desc: Validates Input
+    # Usage: validateInput [str input] [str input type]
+    # 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()
+
+    local fn argInput argType
+
+    # Save function name
+    fn="${FUNCNAME[0]}";
+
+    # Process arguments
+    argInput="$1";
+    argType="$2";
+    if [[ $# -gt 2 ]]; then yell "ERROR:$0:$fn:Too many arguments."; exit 1; fi;
+
+    # Check for blank
+    if [[ -z "$argInput" ]]; then return 1; fi
+    
+    # Define input types
+    ## ssh_pubkey
+    ### Check for alnum/dash base64 (ex: "ssh-rsa AAAAB3NzaC1yc2EAAA")
+    if [[ "$argType" = "ssh_pubkey" ]]; then
+       if [[ "$argInput" =~ ^[[:alnum:]-]*[\ ]*[[:alnum:]+/=]*$ ]]; then
+           return 0; fi; fi;
+
+    ## age_pubkey
+    ### Check for age1[:bech32:]
+    if [[ "$argType" = "age_pubkey" ]]; then
+       if [[ "$argInput" =~ ^age1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]*$ ]]; then
+           return 0; fi; fi
+
+    ## integer
+    if [[ "$argType" = "integer" ]]; then
+       if [[ "$argInput" =~ ^[[:digit:]]*$ ]]; then
+           return 0; fi; fi;
+
+    ## time element (year, month, week, day, hour, minute, second)
+    if [[ "$argType" = "time_element" ]]; then
+       if [[ "$argInput" = "year" ]] || \
+              [[ "$argInput" = "month" ]] || \
+              [[ "$argInput" = "week" ]] || \
+              [[ "$argInput" = "day" ]] || \
+              [[ "$argInput" = "hour" ]] || \
+              [[ "$argInput" = "minute" ]] || \
+              [[ "$argInput" = "second" ]]; then
+           return 0; fi; fi;
+    
+    # Return error if no condition matched.
+    return 1;
+} # Validates strings
 
 magicInitWorkingDir() {
     # Desc: Determine temporary working directory from defaults or user input
@@ -756,6 +810,12 @@ magicParseProcessStrings() {
     local rawFileExt
 
     vbm "STATUS:Starting magicParseProcessStrings() function.";
+    vbm "var:optionProcString:$optionProcString";
+    vbm "var:optionNoStoreRaw:$optionNoStoreRaw";
+    vbm "var:optionStoreRaw:$optionStoreRaw";
+    vbm "var:argRawFileExt:$argRawFileExt";
+    vbm "ary:argProcStrings:${argProcStrings[*]}";
+    vbm "ary:argProcFileExts:${argProcFileExts[*]}"
     # Validate input
     ## Validate argRawFileExt
     if [[ "$argRawFileExt" =~ ^[.][[:alnum:]]*$ ]]; then
@@ -799,8 +859,8 @@ magicParseProcessStrings() {
        if [[ -z "$element" ]]; then yell "ERROR:Empty output file extension specified. Exiting."; exit 1; fi; done
     ## Make sure that no process string starts with '-' (ex: if only one arg supplied after '-p' option)
     for element in "${argProcStrings[@]}"; do
-       if [[ ! "$element" =~ ^[-][[:print:]]*$ ]] && [[ "$element" =~ ^[[:print:]]*$ ]]; then
-           yell "ERROR:Illegal character '-' at start of process string element. Option syntax error?";
+       if [[ "$element" =~ ^[-][[:print:]]*$ ]] && [[ ! "$element" =~ ^[[:print:]]*$ ]]; then
+           yell "ERROR:Illegal character '-' at start of process string element:\"$element\"";
            exit 1; fi; done;
     vbm "STATUS:Quick check shows argProcStrings and argProcFileExts appear to have valid contents.";
     procStrings=("${argProcStrings[@]}"); # Export process command strings