My script is not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting My script is not working
# 1  
Old 03-20-2017
My script is not working

Hi ,

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

Code:
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

Code:
echo $file_date giving 
201701
201702
201703
201704

Code:
echo $cur_yr_month giving 
201701
201702
201703

But still the if condition is not satisfying.

Please help me.

Thanks in advance.

Last edited by jim mcnamara; 03-20-2017 at 08:36 AM.. Reason: Scrambled code; hard to read
# 2  
Old 03-20-2017
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 ?

Code:
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.
# 3  
Old 03-20-2017
Hi,

We are using ksh.

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

Code:
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?

Code:
echo $file_date giving 
201704
201702
201703
201701


Code:
echo $cur_yr_month giving 
201701
201702
201703

Thanks
# 4  
Old 03-20-2017
Given $file_date's contents, as you post, is
Code:
201701
201702
201703
201704

and $cur_yr_month's is
Code:
201701
201702
201703

, of course "the if condition is not satisfying" as the two don't match.
# 5  
Old 03-20-2017
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

Code:
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
# 6  
Old 03-20-2017
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.
# 7  
Old 03-20-2017
Hi,

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

201704
201702
201703
201701

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with speeding up my working script to take less time - how to use more CPU usage for a script

Hello experts, we have input files with 700K lines each (one generated for every hour). and we need to convert them as below and move them to another directory once. Sample INPUT:- # cat test1 1559205600000,8474,NormalizedPortInfo,PctDiscards,0.0,Interface,BG-CTA-AX1.test.com,Vl111... (7 Replies)
Discussion started by: prvnrk
7 Replies

2. UNIX for Beginners Questions & Answers

Why is this script not working?

for file in $(find /home/p611568/*.rpt -mtime -2 | grep -v ftplog) do awk '$1 == "XifPages," {PAGE = $5} $1 == "XifEndOfDocuments," {LINE = $5} index($0,"Value") && index($1,"Info") {sevGTI = 1} END {if (sevGTI) printf "%7s%10s%s",PAGE,LINE ... (4 Replies)
Discussion started by: bcarosi
4 Replies

3. Shell Programming and Scripting

Script not working

Hi i have write the one scripts and the scripts is error. The scripts purpose select one directory to check the file is there or not. i will give the two format of file to search the mention the path one file is there to select the file one copy the another location.please check the my script give... (1 Reply)
Discussion started by: rajivgandhi
1 Replies

4. UNIX for Dummies Questions & Answers

My script is not working

Hi All, I am trying to calculate the total_amount based on two key fields. Here is the script. I am new to unix. So please help me to find out the error in this script. I'm getting error like "command not found" and ]; is missing.. PRE_LBNO=0 PRE_DATE=0 TOT_PAY=0 for line in `cat... (4 Replies)
Discussion started by: lathanandhini
4 Replies

5. Shell Programming and Scripting

expect script inside shell script not working.

Shell Scipt: temp.sh su - <$username> expect pass.exp Expect script: pass.exp #!/usr/bin/expect -f # Login ####################### expect "Password: " send "<$password>\r" it comes up with Password: but doesnt take password passed throguh file. (2 Replies)
Discussion started by: bhavesh.sapra
2 Replies

6. Shell Programming and Scripting

Script not working in cron but working fine manually

Help. My script is working fine when executed manually but the cron seems not to catch up the command when registered. The script is as follow: #!/bin/sh for file in file_1.txt file_2.txt file_3.txt do awk '{ print "0" }' $file > tmp.tmp mv tmp.tmp $file done And the cron... (2 Replies)
Discussion started by: jasperux
2 Replies

7. Shell Programming and Scripting

Script is not working from cron while working manually

Hello, I am facing a very strange problem when I run my script manuallu ./Fetchcode which is using to connect with MKS integrity from linux end it workks fine but when I run it from cron it doesn't work.Can someone help me 1) How could I check my script when it is running from cron like... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

8. Shell Programming and Scripting

HELP !! script not working..

Hi all, I have a piece of code which cork perfect. It copy the no of lines from some files (*.log) into finalLog file. It read the lines after the line have text "ntot". Here it is : #!/bin/bash ... (10 Replies)
Discussion started by: nrjrasaxena
10 Replies

9. Shell Programming and Scripting

Perl script 'system' linking to local shell script not working

Trying to figure out why this works: printpwd.pl #!/usr/bin/perl use CGI::Carp qw( fatalsToBrowser ); print "Content-type: text/html\n\n"; $A = system("pwd"); $A = `pwd`; print "$A\n"; ^^actually that works/breaks if that makes any sense.. i get the working directory twice but when... (5 Replies)
Discussion started by: phpfreak
5 Replies

10. Shell Programming and Scripting

Script not working..."sort" not working properly....

Hello all, I have a file - 12.txt cat 12.txt =============================================== Number of executions = 2 Total execution time (sec.ms) = 0.009883 Number of executions = 8 Total execution time (sec.ms) = 0.001270 Number of... (23 Replies)
Discussion started by: Rahulpict
23 Replies
Login or Register to Ask a Question