# help with multiple loops in shell script

**URL:** <https://community.unix.com/t/help-with-multiple-loops-in-shell-script/306955>\
**Category:** Shell Programming and Scripting\
**Created:** [March 21, 2012, 11:16pm UTC](https://community.unix.com/t/help-with-multiple-loops-in-shell-script/306955 "2012-03-21T23:16:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Irishboy24](https://community.unix.com/letter_avatar/irishboy24/32/6_22734026db12803ebb3e059622ac3534.png) [@Irishboy24](https://community.unix.com/u/Irishboy24)\
**Post date:** [March 21, 2012, 11:16pm UTC](https://community.unix.com/t/help-with-multiple-loops-in-shell-script/306955/1 "2012-03-21T23:16:33Z")

</div>

Hi Guys-

I'm trying to write a script which takes date as input (mm.yy.dd) and search in the current file. If pattern doesn't exist it will then look in a backup directory and so on.

being a newb i'm unable to loop over to the backup directory. hoping for some ideas, i've highlighted the area where i'm stuck . any advice is appreciated.  
I have about mutiple backup directories in the root\_dir and the script should keep searching until it runs out of directories.

the naming convention of backup directories is backup-yyyy.mm.dd

```nohighlight

#!bin/bash

datetime=$1
root_dir=/home/xyz
bkup_dir=`ls -tr | grep -i "backup-" | tail -1
pattern=`grep "$datetime" $root_dir/logfile

if [$pattern = ""]
then "go into bkup folder and search for pattern "
else
   echo -e $pattern
fi

```

---

<div class="post-metadata">

**Author:** ![itkamaraj](https://community.unix.com/user_avatar/community.unix.com/itkamaraj/32/1880_2.png) [@itkamaraj](https://community.unix.com/u/itkamaraj)\
**Post date:** [March 21, 2012, 11:37pm UTC](https://community.unix.com/t/help-with-multiple-loops-in-shell-script/306955/2 "2012-03-21T23:37:02Z")

</div>

"go into bkup folder and search for pattern "

to

```nohighlight
 
find $bkup_dir -type f | xargs grep -l "$datetime"

```
