KCR
KCR

Reputation: 93

Script keep on running for hours and hours

I am trying to count the number of delimiter (semicolon or comma or pipe) in the 2nd and 3rd line of the file. If the count is not matching the file should move to the rejected folder. When the file is in the location "pathname=/opt/interfaces/sample_check/mvfiles/inbox" the script is working fine. But when the file is not in the location the script is keep on running for hours and hours until force abort it. Am I missing something, can you please help.

pathname=/opt/interfaces/sample_check/mvfiles/inbox

findresult=$(find $pathname -type f ( -name "messagemulti.csv" -or -name "messagesemi.txt" -or -name "comma2.txt" -or -name "messagepipe.txt" -or -name "tokkalodi.txt" -or -name "ADMC_POSITION-LT3213.csv" -or -name "DMC_CASHFLOW248.csv" -or -name "ADMC_EQBASKET-WEIGHTS_52387.csv" -or -name "ADMC_POSITION-DDD7.csv" -or -name "ADMC_POSITION-DDD7.csv" ))


Count=`sed -n 2p $findresult | tr '[,]' '\n' | tr '[|]' '\n' | tr '[;]' '\n' | wc -l`
Count2=`sed -n 3p $findresult | tr '[,]' '\n' | tr '[|]' '\n' | tr '[;]' '\n' | wc -l`

echo $Count
echo $Count2
#if the delimiter count from the 2nd line and 3rd line doesnt match the    file will move to the rejected folder
if [ $Count != $Count2 ]

 then echo "Mis Match"

 mv $findresult /opt/interfaces/sample_check/mvfiles/reject
 else echo "Match"
 exit
 fi

Upvotes: 0

Views: 94

Answers (2)

Walter A
Walter A

Reputation: 20002

Test the result before counting. I also made some small changes:

if [ -n "${findresult}" ]; then
   Count=$(sed -n 2p ${findresult}  | tr ',|;' '\n' | wc -l)
   Count2=$(sed -n 3p ${findresult} | tr ',|;' '\n' | wc -l)
fi

Perhaps you want to avoid wc:

 if [ -n "${findresult}" ]; then
    str1=$(sed -n '2s/[^,|;]//gp' ${findresult})
    str2=$(sed -n '3s/[^,|;]//gp' ${findresult})
 fi
 if [ ${#str1) -ne ${#str2} ]; then

Upvotes: 1

monok
monok

Reputation: 585

Add the /dev/null to your find result:

findresult="$(find $pathname -type f ( .... )) /dev/null" 

so, sed will get additionally something empty to work on instead of waiting for user input

Upvotes: 1

Related Questions