Variable Validation

Hi Guys,

I am getting count from a file (pipe delimited).

"23"|"9896"|"20090101"|"New Load"

I am using the following code to get the first two counts.

CtrlFileByte=`/bin/gawk -F"|" '{ print $1 }' $ControlFile`;
CtrlFileCnt=`/bin/gawk -F"|" '{ print $2 }' $ControlFile`;

But the problem is file Byte is not mandatory as well as file count.

So, if it is present then i have execute a check otherwise i just skip it an d check for the other one.

I am using the following code for it. But it is throwing an error.

if [ `echo $CtrlFileCnt|tr -d ' '` = '' ] && [ `echo $SrcFileByte|tr -d ' '` = '' ] ; then
echo "File Count Check and Byte Count Check is Skipped";
else
if [ `echo $CtrlFileCnt|tr -d ' '` = '' ]; then
 echo "File Count Check is skipped";
 else 
 if [ `echo $SrcFileByte|tr -d ' '` != '' ] ; then
 echo "File Byte Check is skipped";
  else
  echo "Both are checked";
fi
fi
fi

But it is working fine when both variables are having values. But when one of them is misssing, then it is throwing an error.

Can somebody help me out here..

Cheers!!!!!!!!!!!!

This code is working :slight_smile:

if [ "${CtrlFileCnt}" = "" ] && [ "${SrcFileByte}" = "" ] ; then
echo "File Count Check and Byte Count Check is Skipped";
elif [ "${CtrlFileCnt}" = "" ]; then
 echo "File Count Check is skipped";
elif [ "${SrcFileByte}" = "" ] ; then
 echo "File Byte Check is skipped";
  else
  echo "Both are checked";
fi

Ah, you've updated the post while I was working on your code. Anyway, I'll post my solution too.

I presume you want to do these checks over all lines of an inputfile containing the pipe delimited data, and not just one line. In such case, your awk statement would greedily fetch all the $1 and $2's of inputfile.

#! /bin/bash

while IFS='|' read a b c
do
    SrcFileByte=${a//\"/}
    CtrlFileCnt=${b//\"/}
    if [ -z $CtrlFileCnt ] && [ -z $SrcFileByte ]; then
        echo "File Count Check and Byte Count Check is Skipped";
    else
        if [ -z $CtrlFileCnt ]; then
            echo "File Count Check is skipped";
        else 
            if [ -z $SrcFileByte ]; then
                echo "File Byte Check is skipped";
            else
                echo "Both are checked";
            fi
        fi
    fi
done < tab_delimited_inputfile

Maybe this will help as it illustrates parsing a line on a delimiter and assigning the values to variables which are then check for null. It saves you some processes and command substitutions too. It does not strip the double-quotes as you seem to just want to test for the presence of something in that field position (you should really make sure its a number, and thus a valid count, etc).

Data file:

$ cat efs.dat
"23"|"9896"|"20090101"|"New Load"
"24"|"9896"|"20090101"|"New Load"
||"20090101"|"New Load"
"26"|"9899"|"20090101"|"New Load"
|"9900"|"20090101"|"New Load"
"27"|"9899"|"20090101"|"New Load"
"28"||"20090101"|"New Load"
#!/bin/ksh

INFILE=efs.dat

#  Read a pipe delimited file, setting variables. Quotes preserve spaces.
while IFS="|" read "CtrlFileByte" "CtrlFileCnt" "the_rest"
do

  print "[$CtrlFileByte] [$CtrlFileCnt] [$the_rest]"

  # -n = check for not null, -z = check for null.
  # Specifically test for each condition and let the else be
  # unexpected errors.
  if [[ -n $CtrlFileByte ]] && [[ -n $CtrlFileCnt ]]; then
    print "Both checked"
  elif [[ -z $CtrlFileByte ]] && [[ -z $CtrlFileCnt ]]; then
    print "Both Skipped"
  elif [[ -z $CtrlFileByte ]]; then
    print "byte check skipped"
  elif [[ -z $CtrlFileCnt ]]; then
    print "count check skipped"
  else  # Always expect the unexpected!
    print "Something unexpected happened"
  fi
done < $INFILE

exit 0

Output:

$ efs
["23"] ["9896"] ["20090101"|"New Load"]
Both checked
["24"] ["9896"] ["20090101"|"New Load"]
Both checked
[] [] ["20090101"|"New Load"]
Both Skipped
["26"] ["9899"] ["20090101"|"New Load"]
Both checked
[] ["9900"] ["20090101"|"New Load"]
byte check skipped
["27"] ["9899"] ["20090101"|"New Load"]
Both checked
["28"] [] ["20090101"|"New Load"]
count check skipped
$