My script is not working

Hi ,

In the script if condition is not working.
Even we have data which is satisfying the condition.

month=1
ls -d db/files/* > /dm/file_names.txt
while [ ${month} -lt $(date +%m) ]; 
do
   cat /dm/file_names.txt | while read f;
   do
            if [ 1 -eq "${#month}" ] ; then
               day="0$month"
            fi
           file_date=`echo $f | cut -d"/" -f10 | cut -d"_" -f4 | cut -d"-" -f1`
           cur_yr_month=`echo $(date +%Y)${day}`
           if [ "$cur_yr_month" = "$file_date" ];
           then
              echo "$file_date"
           fi
          ((month++))
    done
done
echo $file_date giving 
201701
201702
201703
201704
echo $cur_yr_month giving 
201701
201702
201703

But still the if condition is not satisfying.

Please help me.

Thanks in advance.

Hi,

If this is Bash, then equivalence is normally checked with a double equals sign ( == ), rather than a single equals sign, which is normally used for assignment.

So try this instead and see if it helps, maybe ?

if [ "$cur_yr_month" == "$file_date" ];

If you're using something other than Bash or if this doesn't help, then if you can reply with a bit more info about the full nature of the environment in which the script is running (OS and shell, mainly), then we can go from there.

Hi,

We are using ksh.

I have tried with == still it's not working.

Linux 2.6.32-573.1.1.el6.x86_64 #1 SMP Tue Jul 14 02:46:51 EDT 2015 x86_64 x86_64 x86_64 GNU/Linux 

Thanks

---------- Post updated at 07:12 AM ---------- Previous update was at 06:42 AM ----------

This data is not in sorting order that could be the reason?

echo $file_date giving 
201704
201702
201703
201701
echo $cur_yr_month giving 
201701
201702
201703

Thanks

Given $file_date 's contents, as you post, is

201701
201702
201703
201704

and $cur_yr_month 's is

201701
201702
201703

, of course "the if condition is not satisfying" as the two don't match.

Hi,

Could you please help me how to resolve this issue.

Thanks

---------- Post updated at 08:45 AM ---------- Previous update was at 08:13 AM ----------

Hi,

I tried sorting the data.But sorting is not happening.
I have added | sort -n

month=1
ls -d db/files/* > /dm/file_names.txt
while [ ${month} -lt $(date +%m) ]; 
do
   cat /dm/file_names.txt | while read f;
   do
            if [ 1 -eq "${#month}" ] ; then
               day="0$month"
            fi
           file_date=`echo $f | cut -d"/" -f10 | cut -d"_" -f4 | cut -d"-" -f1 | sort -n`
           cur_yr_month=`echo $(date +%Y)${day}`
           if [ "$cur_yr_month" = "$file_date" ];
           then
              echo "$file_date"
           fi
          ((month++))
    done
done

Thanks

What's the exact contents of the two variables, char by char, byte by byte? And, post the contents of /dm/file_names.txt , best as a hex (or similar) dump.

Hi,

The contents of the file is dm/file_names.txt.

201704
201702
201703
201701

Thanks

Moderator comments were removed during original forum migration.