Variable Validation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable Validation
# 1  
Old 03-06-2012
Variable Validation

Hi Guys,

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

Code:
"23"|"9896"|"20090101"|"New Load"

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

Code:
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.

Code:
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 Smilie

Code:
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


Last edited by mac4rfree; 03-06-2012 at 11:41 AM..
# 2  
Old 03-06-2012
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.

Code:
#! /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

# 3  
Old 03-06-2012
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:
Code:
$ 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"

Code:
#!/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:
Code:
$ 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
$


Last edited by gary_w; 03-06-2012 at 12:17 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Validation using While and IF

I am learning Shell scripting on own. I am trying to do an assignment to get details from the user like username their individual marks ,DOB and send a report in mail with the Details calculated like total and average. validate_marks() { local Value=$1 if && then return 0 else... (1 Reply)
Discussion started by: JayashreeRobin
1 Replies

2. Shell Programming and Scripting

Value validation

Hi All I am trying to validate a value using if condition requirement is need to check whether its a valid numeric value the input contains ( space, #N/A and negative and positive decimal values and Zeros) if it contains the space, I need to display the error message as space ... (15 Replies)
Discussion started by: tsurendra
15 Replies

3. Shell Programming and Scripting

File validation

Hi there, As a part of file validation, I needed to check for delimiter count in the file. My aim is to find, how many records have failed to have predefined numbers of delimiters in the file. My code looks like below i=`awk -F '|' 'NF != 2 {print NR, $0} ' ${pinb_fldr}/${pfile}DAT |... (3 Replies)
Discussion started by: anandapani
3 Replies

4. Shell Programming and Scripting

Validation of variable

Hey people, I got small little error which says I entered too many argument in the if else list. The stock contains a list of information. The 4th field consist of the quantity available, so I'm trying to alert the user that the stock is less than 3 and needed to restock. Is this the correct way... (7 Replies)
Discussion started by: aLHaNz
7 Replies

5. Shell Programming and Scripting

Name validation

Hi All, I need to write a small piece of code to check the following. name should contain (A-Z), spaces, hyphens & apostrophes I need to generate regular expressions for the same. Please help me out as i am not familiar with regular expressions. (1 Reply)
Discussion started by: lifzgud
1 Replies

6. Shell Programming and Scripting

Validation help

Hi, I am new to Unix shell scripting and need help to add some validation to an existing script. I've made a script that takes two argument (input) but I want the script to display an error message when nothing (null) is entered. So far I managed to validate the fist argument but fail to... (2 Replies)
Discussion started by: zen10
2 Replies

7. Web Development

Validation in php help !

I am validating a form using php script and I want to "echo" the error message near to the text box itself & not below all the controls....Can I Position the display messages ?..Pls help me... (2 Replies)
Discussion started by: gameboy87
2 Replies

8. UNIX for Dummies Questions & Answers

Validation

I'm kinda new in shell scripting. How do i validate an input from a user to conform to requirement. For example, echo "Enter First Name: " read FName echo "Enter Date of Employment (dd/mm/yyyy): " read DoE If the user enters data that is alphanumeric, it accepts it. I hope i've... (1 Reply)
Discussion started by: Allenzo
1 Replies

9. UNIX for Dummies Questions & Answers

validation on a parameter

Hi guys! Im tryin to set up a login script that allows a user to login with only the following "forename.surname" . The username all contain lowercase letters so thats not a problem to sort out, but im having problems making sure that they can only enter 1 " . " . the code i have so far is this... (1 Reply)
Discussion started by: Henley55
1 Replies

10. Shell Programming and Scripting

Input validation ?

I am trying to validate user input, at the moment what i have is as below :- echo "\n\tPlease, enter package name to import: \c" read PACKAGE So far this works perfect , But if the user does not input any thing it stalls :( What I need is, If the user does not enter the name of the... (9 Replies)
Discussion started by: systemali
9 Replies
Login or Register to Ask a Question