0

I'm writting a scsript to automate some check at work & i have trouble on only 1 standing issue which is the script never ending.

It do the all task over & over. There are 2 while loop & once finishe the first one it logicaly start the 2nd one. Once 2nd one it's done it start back on the first 1.

Also i would like to make it end once the both loop are finished 1 after the other. I'm not doing script very often and the answer could possibly be easy but i cannot find one that suit my script so far.

Thank you for your help.

Below the script:

#!/bin/ksh
#
#
#
#
day=`date +%Y%m%d`
BASEDIR=$(dirname $(readlink -f $0))
cd $BASEDIR

cd /export/home/prodwww/ftp_backup/conf
grep "mds_lin_m" *|cut -d " " -f1 > /export/home/prodwww/Production/Check/tmp/mono_list_"$day".log

cat /export/home/prodwww/Production/Check/tmp/mono_list_"$day".log |cut -d "    " -f1 > /export/home/prodwww/Production/Check/log/mono_list_"$day"_.log
sed -i -e "s/:/\//g" /export/home/prodwww/Production/Check/log/mono_list_"$day"_.log

cat /export/home/prodwww/Production/Check/log/mono_list_'date'_.log |while read lines
do
grep -HL 'NbChannel' /export/home/prodwww/ftp_backup/data/"$lines"/mds.ini >>  /export/home/prodwww/Production/Check/log/result_"$day"_.log;
done

cat  /export/home/prodwww/Production/Check/log/result_"$day"_.log|cut -d"/" -f 7-8>  /export/home/prodwww/Production/Check/log/autoswitch-list.log
sed -i -e "s/\// @ /g" /export/home/prodwww/Production/Check/log/autoswitch-list.log

awk -F" " '{print $3 $2 $1}' /export/home/prodwww/Production/Check/log/autoswitch-list.log > /export/home/prodwww/Production/Check/log/check-list.log
        cat  /export/home/prodwww/Production/Check/log/check-list.log |while read target
do
echo "$target">/export/home/prodwww/Production/Check/log/account.txt

cat /export/home/prodwww/Production/Check/log/account.txt|cut -d"@" -f1>/export/home/prodwww/Production/Check/log/login.txt
logon=$(cat /export/home/prodwww/Production/Check/log/login.txt)
echo $target>>/export/home/prodwww/Production/Check/data/autoswitch_check_"$day".log ;  /usr/local/bin/rww CMD "$target" "ls -lrt /export/home/"$logon"/mds/conf/autoswitch.ini">> /export/home/prodwww/Production/Check/data/autoswitch_check_"$day".log

done
exit
1
  • 3
    Please edit your question and i) give us a minimal version of your script that reproduces the problem. Start removing commands from your script until you find the minimal set that still shows the same behavior. ii) tell us exactly how you run the script, it doesn't make sense that it would move back to a previous loop, so it might be that you are launching it wrong somehow. iii) ideally, also provide a simple data set we can use to reproduce the issue. There's nothing super obviously wrong apart from some bad coding practices, so you need to do some debugging first.
    – terdon
    Commented Sep 14, 2022 at 15:19

1 Answer 1

1

There is no obvious reason for any loop to become "infinite". However, re-writing the exact same script but extracting each file name to a variable we get a more legible version, IMO.

day=`date +%Y%m%d`
BASEDIR=$(dirname $(readlink -f $0))
cd $BASEDIR


dir1="/export/home/prodwww/ftp_backup/conf"
log1="/export/home/prodwww/Production/Check/tmp/mono_list_$day.log"
log2="/export/home/prodwww/Production/Check/log/mono_list_$day_.log"
log3="/export/home/prodwww/Production/Check/log/mono_list_date_.log"
ini1="/export/home/prodwww/ftp_backup/data/$lines/mds.ini"
log4="/export/home/prodwww/Production/Check/log/result_$day_.log"
log5="/export/home/prodwww/Production/Check/log/autoswitch-list.log"
log6="/export/home/prodwww/Production/Check/log/check-list.log"
txt1="/export/home/prodwww/Production/Check/log/account.txt"
txt2="/export/home/prodwww/Production/Check/log/login.txt"
log7="/export/home/prodwww/Production/Check/data/autoswitch_check_$day.log" 
ini2="/export/home/$logon/mds/conf/autoswitch.ini"
log8="/export/home/prodwww/Production/Check/data/autoswitch_check_$day.log"



cd "$dir1"
grep "mds_lin_m" * | cut -d " " -f1 >"$log1"

cat "$log1" | cut -d "    " -f1 >"$log2"
sed -i -e "s/:/\//g" "$log2"

cat "$log3"| while read lines
do
    grep -HL 'NbChannel' "$ini1" >> "$log4"
done

cat  "$log4" | cut -d"/" -f 7-8 > "$log5"
sed -i -e "s/\// @ /g" "$log5"

awk -F" " '{print $3 $2 $1}'  "$log5"> "$log6"
cat  "$log6" | while read target
do
    echo "$target" > "$txt1"

    cat "$txt1" | cut -d"@" -f1 > "$txt2" 
    logon=$( cat "$txt2" )
    echo $target >> "$log7";
    /usr/local/bin/rww CMD "$target" "ls -lrt $ini2" >> "$log7" 

done
exit

And then, it seems obvious that repeating the exact same command grep -HL 'NbChannel' "$ini1" >> "$log4" for each line in log3 will put a lot of repeating lines in log4.

Maybe that is not what you want. And that is the reason that all the following command are repeated over and over again on the same values from log4.

Note that log2 should always be empty as log1 is cut on the first space. Then log1 will have no spaces. It is not possible for cut -d " " -f1 to match anything.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .