Link to home
Start Free TrialLog in
Avatar of antvu
antvu

asked on

test condition in If ..else.. loop not working

Hello All.

I have a script which basically reads a file and moves the files between 2 directories. I am attaching the code. This script has a for loop which reads a file (a pipe delimited file) and with in the for loop , I have a if and else loop , which is not working..

The for loop is working fine. I echoed all the variables which are output from "awk" commands, but it is the if block that is not executing. It is directly going into else block.

is there any glaring syntax error that i am ovelooking.. please help.

Thanks
Antv

# Access the file and read the content to parse the lines to get all values
myfile="$1"
 
echo $myfile
 
for vars in $myfile
  do
    filenm1=`awk -F "|" '{print $1}' $myfile`
    fileid=`awk -F "|" '{print $2}' $myfile`
    filetype=`awk -F "|" '{print $3}' $myfile`
    filedate=`awk -F "|" '{print $4}' $myfile`
    filnm2=`awk -F "|" '{print $5}' $myfile`
    Incoming_ID=`awk -F "|" '{print $6}' $myfile`
 
# Move the files basefd on filetypes
if [[ "$filetype" = "XYZ" ]]; then
    echo "the value of $filetype \n"
     echo " the filesname to be moved is $filenm2. \n"
     echo " filename1 is $filenm1 ...\n"
    mv -f /home/atc/$filenm2 /work/hold/$filenm1
else
   echo " th value of filetype is $filetype..\n"
   echo " if you are seeing this then the if block is not executed"
fi
done

Open in new window

Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

try

if [ $filetype == "XYZ" ]
can you show sample of your file?
also filenm2 is not set any where
Avatar of antvu
antvu

ASKER

Helo Omar

Thanks for responding. I tried your suggestion and following is what i got

0403-012 A test command parameter is not valid.

what shell are you using ?
Avatar of antvu

ASKER

I corrected the typo - I made it as $filnm2.

As for the sample file - here it is

WL_XYZ_overstock.txt.100|100|XYZ|2009-01-26 12:17:26|WL_XYZ_overstock.txt|729
WL_XYZ_overstock.txt.104|104|XYZ|2009-01-26 12:17:26|WL_XYZ_overstock.txt|729
Avatar of antvu

ASKER

#!/user/bin/ksh

korn shell!
ASKER CERTIFIED SOLUTION
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of antvu

ASKER

Thanks Omarfarid.
Your solution worked.
sorry corrected version:

#!/usr/bin/ksh
#
myfile="$1"
#
echo $myfile
cat $myfile | while read line
do
    filenm1=`echo $line | awk -F"|" '{print $1}'`
    fileid=`echo $line | awk -F"|" '{print $2}'`
    filetype=`echo $line | awk -F"|" '{print $3}'`
    filedate=`echo $line | awk -F"|" '{print $4}'`
    filnm2=`echo $line | awk -F"|" '{print $5}'`
    Incoming_ID=`echo $line | awk -F"|" '{print $6}'`
 
# Move the files basefd on filetypes
   if [ $filetype = XYZ ]; then
        echo "the value of $filetype \n"
        echo " the filesname to be moved is $filnm2. \n"
        echo " filename1 is $filenm1 ...\n"
        mv -f /home/atc/$filnm2 /work/hold/$filenm1
   else
        echo " th value of filetype is $filetype..\n"
        echo " if you are seeing this then the if block is not executed"
   fi
done