Do While Loop Problem.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Do While Loop Problem.
# 1  
Old 11-29-2010
Do While Loop Problem.

Hello,

My below text is not working....from cat keyword onwards. Any idea why?
Is there any syntax problem while fetching the Fd and Fn?
Code:
DATE=`date`
 
cat test.txt | while read line 
do
Fd=`echo $line | awk '{print ($6),($7)}'`
Fn=`echo $line | awk '{print $9}'`
if [ "$Fd" = "$DATE" ] || [ "$Fd" -lt "$DATE" ]; then
gzip $Fn
else 
exit
fi
done

Moderator's Comments:
Mod Comment Please use code tags

Last edited by Scott; 11-29-2010 at 06:51 PM..
# 2  
Old 11-29-2010
Wat is the format of the test.txt file?
Does your system have GNU date (try "date --date today")?
# 3  
Old 11-29-2010
Hi,

I am actually using "date --date 1 month ago", which is fine.
Issue is not here. it seems.

The format is also tab limited, which is find to pick for awk command.
# 4  
Old 11-29-2010
Yes, it's text, but what is the text? Smilie

Also, what shell are you using? I don't think any shell but KSH can compare dates, and even then I'm not sure it can do so in that manner... If you can get your outputs and comparisons in epoch seconds("date +%s" for GNU date) that should work nicely just with plain numeric comparisons.

You've got a useless use of cat in there, and a useless use of awk. awk is a powerful enough language that you could probably write this entire script in it: Running two seperate instances per line is not a good idea. Just using shell builtins is hundreds of times faster for small amounts of data. Rule of thumb is, don't run anything to process a single line, builtins only!

Code:
DATE=`date`
 
while read A1 A2 A3 A4 A5 A6 A7 A8 Fn A10
do
        Fd="${A6} ${A7}"
        # "-lt" is for integers-only, afaik
        if [[ "${Fd}" == "$DATE" ]] || [[ "${Fd}" -lt "$DATE" ]]
        then
                gzip "$Fn"
                continue
        fi

        # Do you really want it to quit every time a file isn't too old?
        exit
done < test.txt


Last edited by Corona688; 11-29-2010 at 07:21 PM..
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 11-29-2010
Thanks. Smilie
One more help on this with if condition:

I need to put,

if [ "${Fd}" = "1month before" ) || [ "${Fd}" -lt "1 month before"]
Then
gzip "$Fn"
else
exit
.........
Now all the dates above are coming in this format: "Nov 30"
I am also able to get 1 month before date in the same format: "Oct 30"
BUT HOW I WILL COMPARE THESE FILE DATE (Fd) and 1month BEFORE DATE IN 'IF' STATEMENT?
HOW MONTHS WILL GET COMPARE?
# 6  
Old 11-29-2010
Use +%s format with date to convert your dates to seconds past epoch. Then it's a simple numeric comparison

eg:

Code:
MONTH_AGO=$(date --date -1month +%s)
while read A1 A2 A3 A4 A5 A6 A7 A8 Fn A10
do
        Fd=$(date --date "${A6} ${A7}" +%s)
        if [ $Fd -le $MONTH_AGO ]
        then
                gzip "$Fn"
                continue
        fi
 
        # Do you really want it to quit every time a file isn't too old?
        exit
done < test.txt

# 7  
Old 11-29-2010
Thanks Smilie Smilie
All done.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with loop

Hi all, a problem with a loop. Imagine it starts with var1="$(cat txtif.out )"while do echo file1 echo file2 echo y > txtif.out if then break fi done exit 0 the problem is that if the file is changing during the loop seems to continue as it reads "x" state,... (5 Replies)
Discussion started by: Board27
5 Replies

2. Shell Programming and Scripting

Problem with While Do loop

I am trying to do a while do loop that asks for a password and returns if the password entered is incorrect. I have that part working however i want to modify it so that if a similar word is entered it will read "that is close but incorrect" and i cannot seem to get it working, being new in the... (6 Replies)
Discussion started by: lm5522
6 Replies

3. Shell Programming and Scripting

Problem with loop within loop

Hi, I work on Ab-initio ETL tool which is based on Unix. I made a small script which has two loop's one with in another. All the functionality is working for the first line of outer loop but when it comes to other lines of outer loop it is throwing error as command not found. Below is the... (4 Replies)
Discussion started by: Ravindra Swan
4 Replies

4. Shell Programming and Scripting

Problem with While loop.

Hi Script Gurus, I am facing issue with while loop in bash. The while loop is running as end less loop even after given criteria does not meet. STATE_BEFORE_SPLIT () { for clone in $Clonegroups; do state=$( $Navicmd -listclone -name $clone -cloneid $Cloneid |awk... (3 Replies)
Discussion started by: RobP
3 Replies

5. UNIX for Dummies Questions & Answers

While loop problem

I have a while loop with -f and -o option.Can any one please tell me what that stands for?The Sample code is as follows:- while do ## some processing done (10 Replies)
Discussion started by: Param0073
10 Replies

6. Shell Programming and Scripting

problem with while loop

hi I had created a while loop in a script file called whiletest.sh as follows as follows: count=0 max=10 while do echo $count echo count=$(count+1)" " done echo "value of count:$count" then i run the script with the command sh whilestest.sh but its giving me an error... (5 Replies)
Discussion started by: angel12345
5 Replies

7. Shell Programming and Scripting

Problem in While loop

Hi Guys, This is my code.. I am getting an error in the inside while statement when comparing.. I am not able to figure it out.. Pls help me... while read value fatherid son_id top_id r_type do inside_value=1; echo $inside_value; echo $r_type; while do echo... (6 Replies)
Discussion started by: mac4rfree
6 Replies

8. UNIX for Advanced & Expert Users

loop problem

Hi guys my while do loop is not working properly; As soon as the load_date and run_date is same it should stop can somebody tell me where I am having the problem? In an oracle table I have LOAD_DT=5/1/2009 DATE datatype RUN_DT=5/5/2009 DATE datatype Now in a script with a spool file I get the... (15 Replies)
Discussion started by: henrysmith
15 Replies

9. Shell Programming and Scripting

for loop problem

Hi, I have a directory called logs in which i have the log files. i have to touch the file before deleting it. i am doing like this filestodelete="*.log* *log*" for files in $filestodelete do touch $files $files.$(date +%a) rm -f $filestodelete done touch is not working... (5 Replies)
Discussion started by: namishtiwari
5 Replies

10. Programming

problem with while loop

hi all, i have written the following code: while(proceed !='Y' && proceed!='N' && proceed!='y' && proceed!='n') { printf("\nPress \n\t 'Y' or 'y' to continue \n\t 'N' or 'n' to cancel:"); scanf("%c",&proceed); } the output i am gettin is: Press 'Y' to continue ... (1 Reply)
Discussion started by: mridula
1 Replies
Login or Register to Ask a Question