How to fix this bug?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to fix this bug?
# 1  
Old 10-16-2013
How to fix this bug?

Hi gurus,

I have script as below:
Code:
#!/bin/ksh
        while :
        do
                cat /dev/null > srcfile
                exit_time=`TZ=GMT-20 date +%Y%m%d1950`
                cur_time=`date +%Y%m%d%H%M`
                flag=1
                if [ ! -f unprocessed ]; then
                        echo "FILE CHECKING STARTING" >  unprocessed
                fi

                while IFS="," read code desc file

                        do

                        echo $file
                        echo $code
                        if [ -f $file* ] ; then
                                ofile=`ls -1tr|head -1`
                                mth=`ls -l $ofile* |awk '{print $6}'`
                                dy=`ls -l $ofile* |awk '{print $7}'`
                                time=`ls -l $ofile* |awk '{print $8}'`
                                echo "$code, $file, $mth, $dy, $time, FOUND"
                                echo "$code, $file, $mth, $dy, $time" >> srcfile
                        else
                                flag=0
                                echo "$code, $file , NOT FOUND" >>notexist
                        fi

                        done < list
                
                echo "Variable flag values is " $flag
                end_time=`date +"%m-%d-%Y:%T"`

                if [ $flag -eq 1 ]; then
                        echo "ALL FEED FOUND" >notexist
                        echo "File checking complete. All files arrived before $end_time" > filecheckingstatus       
                        exit 0
                else
                        if [ $cur_time -ge $exit_time ]; then
                                cat unprocessed >> filecheckingstatus
                                exit 20
                        fi
                fi

                echo "Current day and time is " `date +%d%H%M`

                sleep 120

                echo "Current day and time is " `date +%d%H%M`
        done

in my file "list" only one record and there is no this file in current dir.
Code:
abc,aaa,file1

when running debug, I got below:
Code:
+ cat /dev/null
+ date +%Y%m%d1950
TZ=GMT-20
exit_time=201310171950
+ date +%Y%m%d%H%M
cur_time=201310161238
flag=1
+ [ ! -f unprocessed ]
IFS=,
+ read code desc file
+ echo file1
file1
+ echo abc
abc
+ [ -f file1* ]
+ cat /dev/null
flag=0
+ echo abc, file1 , NOT FOUND
IFS=,
+ read code desc file
+ echo Variable flag values is  1
Variable flag values is  1
+ date +%m-%d-%Y:%T
end_time=10-16-2013:12:38:40
+ [ 1 -eq 1 ]
+ echo ALL FEED FOUND
+ echo File checking complete. All files arrive on time
File checking complete. All files arrive on time
+ echo File checking complete. All files arrived before 10-16-2013:12:38:40
+ exit 0

for the value of $flag, it should be 0, but it return 1 in my current script.
Is this because the while loop has one more read (see highlighted in blue) which causes the variable has no value been assigned, so it pick up ini value?
any body can help this

Thanks in advance.

Last edited by vbe; 10-16-2013 at 01:53 PM..
# 2  
Old 10-16-2013
This construct is a bit strange to me:
Code:
                while IFS="," read code desc file

                        do

or is it I spent too much time in debugging xml stuff?
# 3  
Old 10-16-2013
At first glance, the reversion in $flag's value suggests that the inner while-loop is running in a subshell environment, but that shouldn't be the case.

Exactly which shell version and operating system is this script run on?

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 4  
Old 10-16-2013
Quote:
Originally Posted by alister
At first glance, the reversion in $flag's value suggests that the inner while-loop is running in a subshell environment, but that shouldn't be the case.

Exactly which shell version and operating system is this script run on?

Regards,
Alister
I got it, the reason is different shell

Thank you very much
# 5  
Old 10-16-2013
Quote:
Originally Posted by ken6503
I got it, the reason is different shell
Can you be more specific regarding the source of the problem and the solution? As it stands, your last post would not be of any help to someone else confronted with the same problem. And, personally, I'm curious how that while-loop landed in a subshell (if that is indeed what happened).

Regards,
Alister
# 6  
Old 10-17-2013
Quote:
Originally Posted by alister
Can you be more specific regarding the source of the problem and the solution? As it stands, your last post would not be of any help to someone else confronted with the same problem. And, personally, I'm curious how that while-loop landed in a subshell (if that is indeed what happened).

Regards,
Alister
to be honest, I don't know why.
when I first run the script I use:
sh scriptname
it gave me error.
when using ksh scriptname
I got correct result
Would you please explain what is different sh xxx and ksh xxx.

Thanks
# 7  
Old 10-17-2013
Many shell versions run a while loop in a sub shell so cannot set global variables.
--
Code:
cat /dev/null > srcfile

is UUOC, can be a simple
Code:
 > srcfile

This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question