1833386 Members
3209 Online
110052 Solutions
New Discussion

Re: While loop stuck

 
SOLVED
Go to solution
Vanquish
Occasional Advisor

While loop stuck

To give a short description what i am doing in the script is reading from a regular text file for the directory structure and changing it to another format and creating the structure, but the problem is once it reads a line from the text file it fails on the second line, I am stuck with the while loop any suggestions please.
Thanks

The REQFILE has
/RIP/module/resources/com/res/app/email/htmlConfirmationEmailHP.txt
RIP/module/resources/com/crm/client/UserMessages.properties
RIP/module/config/html/BrandDetectorServlet.properties


Script:
#!/bin/ksh
STAGEDIR=/home/user/Patch/build_patch/lists
SCRIPTDIR=/home/user/Patch

REQFILE=rip_files.txt
START=(What should I put)
END=`cat $REQFILE|wc -l`
echo $END
while [ $START -lt $END ]
do
for FLPATHNM in `cat $REQFILE`
do
cd $STAGEDIR

FSTCHR=`echo $FLPATHNM|awk '{print substr($0,1,1)}'`
LSTCHR=`echo $FLPATHNM|awk '{print substr($0,length($0),1)}'`
if [ $FSTCHR = "/" ]
then FLPATHNM1=`echo $FLPATHNM|awk '{print substr($0,2,length($0))}'`
else FLPATHNM1=`echo $FLPATHNM`
fi
if [ $LSTCHR = "/" ]
then TFLPATHNM2=`echo $FLPATHNM1|awk '{print substr($0,1,length($0)-1)}'`
else TFLPATHNM2=`echo $FLPATHNM1`

fi
TFLPATHNM4=`echo $TFLPATHNM2|sed 's/RIP\/module\/resources/temp\/RIP\/lib\/resourcepatches/'`
TFLPATHNM4=`echo $TFLPATHNM2|sed 's/RIP\/module\/config/temp\/RIP\/lib\/configpatches/'`

FLPATHNM2=$TFLPATHNM4
LENPATH=`echo $FLPATHNM2|awk '{print length()}'`
SLSHFLG=0
SLSHPOS=X
while [ $SLSHFLG -ne 1 ] && [ $LENPATH -gt 0 ]
do
CHRLKCMP=`echo $FLPATHNM2|awk '{print substr($0,'$LENPATH',1)}'`
if [ $CHRLKCMP = "/" ]
then SLSHFLG=1
SLSHPOS=$LENPATH
fi
((LENPATH = LENPATH - 1))
done
((SLSHPOS = SLSHPOS + 1))
CDPATH=`echo $FILENM|awk '{print substr($0,1,'$LENPATH')}'`
FILENM=`echo $FILENM|awk '{print substr($0,'$SLSHPOS',length())}'`
FSTCHR=`echo $FLPATHNM2|awk '{print substr($0,1,1)}'`
if [ $FSTCHR = "/" ]
then FILENM=`echo $FLPATHNM2|awk '{print substr($0,2)}'`
else FILENM=$FLPATHNM2
fi
LENPATH=`echo $FILENM|awk '{print length()}'`
SLSHFLG=0
SLSHPOS=X
while [ $SLSHFLG -ne 1 ] && [ $LENPATH -gt 0 ]
do
CHRLKCMP=`echo $FILENM|awk '{print substr($0,'$LENPATH',1)}'`
if [ $CHRLKCMP = "/" ]
then SLSHFLG=1
SLSHPOS=$LENPATH
fi
((LENPATH = LENPATH - 1))
done
if [ $SLSHPOS = X ]
then echo "ERROR: "$FILENM": Path Specified Is Incorrect!"
continue
fi
((SLSHPOS = SLSHPOS + 1))
cd /home/user/Patch
CDPATH=`echo $FILENM|awk '{print substr($0,1,'$LENPATH')}'`
echo $CDPATH
FILENM=`echo $FILENM|awk '{print substr($0,'$SLSHPOS',length())}'`
if [ -a $SCRIPTDIR/$CDPATH ]
then cd $SCRIPTDIR/$CDPATH
cp $MODULEDIR/$TFLPATHNM2 $SCRIPTDIR/$CDPATH
else mkdir -p $CDPATH
cp $MODULEDIR/$TFLPATHNM2 $SCRIPTDIR/$CDPATH
continue
fi
echo "-> File: "$FILENM
done
done(end while loop)
7 REPLIES 7
Patrick Wallek
Honored Contributor

Re: While loop stuck

How about:

while read LINE
do
....
all your stuff
....
done < ${REQFILE}

curt larson_1
Honored Contributor
Solution

Re: While loop stuck

well you have

while [ $START -lt $END ]
do
for FLPATHNM in `cat $REQFILE`
do
cd $STAGEDIR
.
.
.
done
done

the value of start and end never change. it is just like putting a while true do around your for loop. it doesn't do anything in this example. so the while start -lt end can be removed completely and the for loop is better written as the previous poster suggested. myself i like to see where my input is coming from at the begining of the while loop instead of the end, so I'd write it this way

cd $STAGEDIR
cat $REQFILE |
while read FLPATHNM
do
.
.
.
done

it is also more efficient to preform the cd only once instead of for everytime through the loop.
curt larson_1
Honored Contributor

Re: While loop stuck

another suggestion would be to learn to use the substring parameter expansions of the shell.

char2on=${FLPATHNM#?}
char1toNextToLast=${FLPATHNM%?}
fchar=${FLPATHNM%$char2on}
lchar=${FLPATHNM#$char1toNextToLast}

if [ $fchar = "/" ]
then FLPATHNM1=$char2on
else FLPATHNM1=$FLPATHNM
fi
if [ $lchar = "/" ]
then TFLPATHNM2=$char1toNextToLast
else TFLPATHNM2=$FLPATHNM1
fi

does the same thing as

FSTCHR=`echo $FLPATHNM|awk '{print substr($0,1,1)}'`
LSTCHR=`echo $FLPATHNM|awk '{print substr($0,length($0),1)}'`
if [ $FSTCHR = "/" ]
then FLPATHNM1=`echo $FLPATHNM|awk '{print substr($0,2,length($0))}'`
else FLPATHNM1=`echo $FLPATHNM`
fi
if [ $LSTCHR = "/" ]
then TFLPATHNM2=`echo $FLPATHNM1|awk '{print substr($0,1,length($0)-1)}'`
else TFLPATHNM2=`echo $FLPATHNM1`

fi

only about 5000 times faster
Duncan Galbraith
Frequent Advisor

Re: While loop stuck

In this instance you do not need the while loop at all as you are going through each entry in your input file in this for loop:
for FLPATHNM in `cat $REQFILE`

I'm sure you are aware that you can't move your:
cd $STAGEDIR
outside the loop as you may or may not change directory later in your for loop and you do want to start at the right place the next time round I guess.
curt larson_1
Honored Contributor

Re: While loop stuck

TFLPATHNM4=`echo $TFLPATHNM2|sed 's/RIP\/module\/config/temp\/RIP\/lib\/configpatches/'`

could be more readable as

TFLPATHNM4=`echo $TFLPATHNM2|sed 's!RIP/module/config!temp/RIP/lib/configpatches!'`

in sed the regular expression can be delimited by any character execpt a blank or a newline. So, if the pattern contains slashes, you could chosse another character, such as an exclamation mark as the delimiter.

and you can put both the subsitutions in the same command

TFLPATHNM4=`echo $TFLPATHNM2|sed -e 's!RIP/module/resources!temp/RIP/lib/resourcepatches!' -e 's!RIP/module/config!temp/RIP/lib/configpatches!'`

LENPATH=${#FLPATHNM2)
is much faster then
LENPATH=`echo $FLPATHNM2|awk '{print length()}'`

SLSHPOS=X
...
((SLSHPOS = SLSHPOS + 1))
....
FILENM=`echo $FILENM|awk '{print substr($0,'$SLSHPOS',length())}'`

I don't know how well using alphabetic characters are going to work when used in numeric contexts. I'd think you'd be better of using only digits.
curt larson_1
Honored Contributor

Re: While loop stuck

not to sure what your doing here

SLSHFLG=0
SLSHPOS=X
while [ $SLSHFLG -ne 1 ] && [ $LENPATH -gt 0 ]
do
CHRLKCMP=`echo $FLPATHNM2|awk '{print substr($0,'$LENPATH',1)}'`
if [ $CHRLKCMP = "/" ]
then SLSHFLG=1
SLSHPOS=$LENPATH
fi
((LENPATH = LENPATH - 1))
done
((SLSHPOS = SLSHPOS + 1))
CDPATH=`echo $FILENM|awk '{print substr($0,1,'$LENPATH')}'`
FILENM=`echo $FILENM|awk '{print substr($0,'$SLSHPOS',length())}'`
FSTCHR=`echo $FLPATHNM2|awk '{print substr($0,1,1)}'`

but your computing LENPATH and SLSHPOS using $FLPATHNM2 then applying it to FILENM


slshpos=${FLPATHNM2%/*}
slshpos=$((${#slshpos} + 1 ))
#lenpath=slshpos
echo $FILENM|awk -v slash=$slshpos '{
path=substr($0,1,slash);
file=substr($0,slash,length());
print path " " file;
}' | read cdpath filenm

does the same thing

and you already removed the leading slash once so it is redunant to do it again
Vanquish
Occasional Advisor

Re: While loop stuck

Thanks a lot Mr Curt Larson, I wish i could think like you may be one day.
Thanks Everyone
Thanks Again Mr Curt Larson