# Do While Loop Problem.

**URL:** <https://community.unix.com/t/do-while-loop-problem/278165>\
**Category:** Shell Programming and Scripting\
**Created:** [November 29, 2010, 4:39pm UTC](https://community.unix.com/t/do-while-loop-problem/278165 "2010-11-29T16:39:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![amit.mathur08](https://community.unix.com/letter_avatar/amit.mathur08/32/5_5575768a8748004e209b776fc1b2916d.png) [@amit.mathur08](https://community.unix.com/u/amit.mathur08)\
**Post date:** [November 29, 2010, 4:39pm UTC](https://community.unix.com/t/do-while-loop-problem/278165/1 "2010-11-29T16:39:19Z")

</div>

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?

```nohighlight
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

```

---

<div class="post-metadata">

**Author:** ![Chubler\_XL](https://community.unix.com/user_avatar/community.unix.com/chubler_xl/32/2077_2.png) [@Chubler\_XL](https://community.unix.com/u/Chubler_XL)\
**Post date:** [November 29, 2010, 5:20pm UTC](https://community.unix.com/t/do-while-loop-problem/278165/2 "2010-11-29T17:20:05Z")

</div>

Wat is the format of the test.txt file?  
Does your system have GNU date (try "date --date today")?

---

<div class="post-metadata">

**Author:** ![amit.mathur08](https://community.unix.com/letter_avatar/amit.mathur08/32/5_5575768a8748004e209b776fc1b2916d.png) [@amit.mathur08](https://community.unix.com/u/amit.mathur08)\
**Post date:** [November 29, 2010, 5:46pm UTC](https://community.unix.com/t/do-while-loop-problem/278165/3 "2010-11-29T17:46:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [November 29, 2010, 6:12pm UTC](https://community.unix.com/t/do-while-loop-problem/278165/4 "2010-11-29T18:12:01Z")

</div>

Yes, it's text, but what is the text? 😃

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](http://partmaps.org/era/unix/award.html) in there, and a [useless use of awk](http://partmaps.org/era/unix/award.html#grep). 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!

```plaintext
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

```

---

<div class="post-metadata">

**Author:** ![amit.mathur08](https://community.unix.com/letter_avatar/amit.mathur08/32/5_5575768a8748004e209b776fc1b2916d.png) [@amit.mathur08](https://community.unix.com/u/amit.mathur08)\
**Post date:** [November 29, 2010, 6:52pm UTC](https://community.unix.com/t/do-while-loop-problem/278165/5 "2010-11-29T18:52:48Z")

</div>

Thanks. 🙂  
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?

---

<div class="post-metadata">

**Author:** ![Chubler\_XL](https://community.unix.com/user_avatar/community.unix.com/chubler_xl/32/2077_2.png) [@Chubler\_XL](https://community.unix.com/u/Chubler_XL)\
**Post date:** [November 29, 2010, 7:48pm UTC](https://community.unix.com/t/do-while-loop-problem/278165/6 "2010-11-29T19:48:29Z")

</div>

Use +%s format with date to convert your dates to seconds past epoch. Then it's a simple numeric comparison

eg:

```nohighlight
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

```

---

<div class="post-metadata">

**Author:** ![amit.mathur08](https://community.unix.com/letter_avatar/amit.mathur08/32/5_5575768a8748004e209b776fc1b2916d.png) [@amit.mathur08](https://community.unix.com/u/amit.mathur08)\
**Post date:** [November 29, 2010, 8:10pm UTC](https://community.unix.com/t/do-while-loop-problem/278165/7 "2010-11-29T20:10:56Z")

</div>

Thanks 🙂 🙂  
All done.
