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

System Programming

Avatar of undefined
Last Comment
omarfarid

8/22/2022 - Mon
omarfarid

try

if [ $filetype == "XYZ" ]
omarfarid

can you show sample of your file?
omarfarid

also filenm2 is not set any where
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER
antvu

Helo Omar

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

0403-012 A test command parameter is not valid.

omarfarid

what shell are you using ?
ASKER
antvu

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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
antvu

#!/user/bin/ksh

korn shell!
ASKER CERTIFIED SOLUTION
omarfarid

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
antvu

Thanks Omarfarid.
Your solution worked.
omarfarid

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
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23