# problem in while loop in a script

**URL:** <https://community.unix.com/t/problem-in-while-loop-in-a-script/216758>\
**Category:** Shell Programming and Scripting\
**Created:** [October 16, 2008, 6:23am UTC](https://community.unix.com/t/problem-in-while-loop-in-a-script/216758 "2008-10-16T06:23:39Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![ali560045](https://community.unix.com/user_avatar/community.unix.com/ali560045/32/698_2.png) [@ali560045](https://community.unix.com/u/ali560045)\
**Post date:** [October 16, 2008, 6:23am UTC](https://community.unix.com/t/problem-in-while-loop-in-a-script/216758/1 "2008-10-16T06:23:39Z")

</div>

i have a script that will read each line and then grep a particular pattern and do some\_stuff. Below the script

while read j  
do  
q1=0  
q1=`$j | grep 'INFO - LPBatch:' | wc -l`  
if [$q1 -eq 1]  
then  
$j | tr -s " " | cut -d " " -f8,42,43 \>\> nav1.txt  
fi

q2=0  
q2=`$j | grep 'INFO - Number of Intervals Not Inserted:' | wc -l`  
if [$q2 -eq 1]  
then  
$j | tr -s " " | cut -d " " -f6-13 \>\> nav1.txt  
fi  
done \< nav11.txt  
-----------------------------------------------------------------------

below some of the lines of the file nav11.txt

2008-10-14 05:47:05,551 [Thread-6] ERROR - CleanLPDataMessage: Missing Intervals: 1

2008-10-14 05:47:05,551 [Thread-6] INFO - LPBatch: [null, 1-GH32X, null, DC:Tue Oct 14 10:12:37 UTC 2008, null, null, Mon Oct 13 05:00:00 CDT 2008, Mon Oct 13 10:00:00 UTC 2008, Tue Oct 14 04:45:00 CDT 2008, Tue Oct 14 09:45:00 UTC 2008, CC, AMR, 94]

2008-10-14 05:47:05,575 [Thread-6] INFO - Number of Intervals Not Inserted: 1 / 95

2008-10-14 05:02:56,762 [Thread-6] ERROR - CleanLPDataMessage: Missing Intervals: 3

2008-10-14 05:02:56,763 [Thread-6] INFO - LPBatch: [null, 1-VIN74, null, DC:Tue Oct 14 09:29:49 UTC 2008, null, null, Mon Oct 13 04:00:00 CDT 2008, Mon Oct 13 09:00:00 UTC 2008, Tue Oct 14 04:00:00 CDT 2008, Tue Oct 14 09:00:00 UTC 2008, CC, AMR, 97]

2008-10-14 05:02:56,763 [Thread-7] INFO - Register Read: [1-X22K8, 0.0, Tue  
------------------------------------------------------------------------

the while part is having some problem.can anyone help me in this?

---

<div class="post-metadata">

**Author:** ![vkesineni](https://community.unix.com/letter_avatar/vkesineni/32/6_449c533f586939b6e5f246632a46421f.png) [@vkesineni](https://community.unix.com/u/vkesineni)\
**Post date:** [October 16, 2008, 6:37am UTC](https://community.unix.com/t/problem-in-while-loop-in-a-script/216758/2 "2008-10-16T06:37:27Z")

</div>

awk '/LPBatch/{print $0}' test1|cut -d" " -f6-13

Try the above. In my test I got the out put like

LPBatch: [null, 1-GH32X, null, DC:Tue Oct 14 10:12:37  
LPBatch: [null, 1-VIN74, null, DC:Tue Oct 14 09:29:49

---

<div class="post-metadata">

**Author:** ![Luka](https://community.unix.com/letter_avatar/luka/32/6_449c533f586939b6e5f246632a46421f.png) [@Luka](https://community.unix.com/u/Luka)\
**Post date:** [October 16, 2008, 6:41am UTC](https://community.unix.com/t/problem-in-while-loop-in-a-script/216758/3 "2008-10-16T06:41:17Z")

</div>

When you store a line of input into a variable you must echo it thru pipe:

```nohighlight
q1=`echo $j | grep 'INFO - LPBatch:' | wc -l`

```

---

<div class="post-metadata">

**Author:** ![ali560045](https://community.unix.com/user_avatar/community.unix.com/ali560045/32/698_2.png) [@ali560045](https://community.unix.com/u/ali560045)\
**Post date:** [October 16, 2008, 6:44am UTC](https://community.unix.com/t/problem-in-while-loop-in-a-script/216758/4 "2008-10-16T06:44:17Z")

</div>

ok. got that but still nothing is comming to o/p file nav1.txt

the while is reading file nav11.txt line by line and then grepping for either 'INFO - LPBatch:' or 'INFO - Number of Intervals Not Inserted:' and if either of them found then it is not going to their respective if condition ?

is anything wrong with the if condition in the while loop?

---

<div class="post-metadata">

**Author:** ![Luka](https://community.unix.com/letter_avatar/luka/32/6_449c533f586939b6e5f246632a46421f.png) [@Luka](https://community.unix.com/u/Luka)\
**Post date:** [October 16, 2008, 6:56am UTC](https://community.unix.com/t/problem-in-while-loop-in-a-script/216758/5 "2008-10-16T06:56:14Z")

</div>

Have you tried running your script with debugging? In sh it's -x

```nohighlight
sh -x script.sh

```

Or add

```nohighlight
set -x

```

at the begining of the script.

---

<div class="post-metadata">

**Author:** ![vishwa787](https://community.unix.com/letter_avatar/vishwa787/32/6_449c533f586939b6e5f246632a46421f.png) [@vishwa787](https://community.unix.com/u/vishwa787)\
**Post date:** [October 16, 2008, 6:59am UTC](https://community.unix.com/t/problem-in-while-loop-in-a-script/216758/6 "2008-10-16T06:59:43Z")

</div>

just try awk '{print \$1}'

---

<div class="post-metadata">

**Author:** ![ali560045](https://community.unix.com/user_avatar/community.unix.com/ali560045/32/698_2.png) [@ali560045](https://community.unix.com/u/ali560045)\
**Post date:** [October 16, 2008, 7:04am UTC](https://community.unix.com/t/problem-in-while-loop-in-a-script/216758/7 "2008-10-16T07:04:53Z")

</div>

yes i did put set -x and got that while is indeed reading line by line but if found grepped pattern not going to their respective if condition. below the debugging o/p:

+ test -f nav1.txt  
+ 0\< asma  
+ read j  
+ q1=0  
+ q2=0  
+ + grep INFO - LPBatch:  
+ echo 2008-10-14 05:47:05,551 [Thread-6] ERROR - CleanLPDataMessage: Missing Intervals: 1  
./w.sh: Resource temporarily unavailable  
q1= 0  
+ [0 -eq 1]  
+ + grep INFO - Number of Intervals Not Inserted:  
+ echo 2008-10-14 05:47:05,551 [Thread-6] ERROR - CleanLPDataMessage: Missing Intervals: 1  
+ wc -l  
q2= 0  
+ [0 -eq 1]  
+ read j  
+ q1=0  
+ q2=0  
+ + wc -l  
+ grep INFO - LPBatch:  
+ echo 2008-10-14 05:47:05,551 [Thread-6] INFO - LPBatch: [null, 1-GH32X, null, DC:Tue Oct 14 10:12:37 UTC 2008, null, null, Mon Oct 13 05:00:00 CDT 2008, Mon Oct 13 10:00:00 UTC 2008, Tue Oct 14 04:45:00 CDT 2008, Tue Oct 14 09:45:00 UTC 2008, CC, AMR, 94]  
q1= 0  
+ [0 -eq 1]  
+ + echo 2008-10-14 05:47:05,551 [Thread-6] INFO - LPBatch: [null, 1-GH32X, null, DC:Tue Oct 14 10:12:37 UTC 2008, null, null, Mon Oct 13 05:00:00 CDT 2008, Mon Oct 13 10:00:00 UTC 2008, Tue Oct 14 04:45:00 CDT 2008, Tue Oct 14 09:45:00 UTC 2008, CC, AMR, 94]  
+ wc -l  
+ grep INFO - Number of Intervals Not Inserted:  
q2= 0  
+ [0 -eq 1]  
+ read j  
+ q1=0  
+ q2=0  
+ + wc -l  
+ grep INFO - LPBatch:  
+ echo 2008-10-14 05:47:05,575 [Thread-6] INFO - Number of Intervals Not Inserted: 1 / 95  
q1= 0  
+ [0 -eq 1]  
+ + echo 2008-10-14 05:47:05,575 [Thread-6] INFO - Number of Intervals Not Inserted: 1 / 95  
+ grep INFO - Number of Intervals Not Inserted:  
+ wc -l  
q2= 0  
+ [0 -eq 1]  
+ read j  
+ echo completed  
completed

---

<div class="post-metadata">

**Author:** ![zaxxon](https://community.unix.com/user_avatar/community.unix.com/zaxxon/32/660_2.png) [@zaxxon](https://community.unix.com/u/zaxxon)\
**Post date:** [October 16, 2008, 7:05am UTC](https://community.unix.com/t/problem-in-while-loop-in-a-script/216758/8 "2008-10-16T07:05:16Z")

</div>

I put in the missing echos to your script and had the $q changed to "${q}" just to make sure the value/strings will be used completely and I got some output in nav1.txt. Here is your changed script.

If you like you can explain what you need from this line so we can have it a bit easier and compact with maybe 1-2 lines of code. Nevertheless, here you go:

```nohighlight
while read j
do
        q1=0
        q1=`echo "${j}" | grep 'INFO - LPBatch:' | wc -l`
        if [$q1 -eq 1]; then
                echo "${j}" | tr -s " " | cut -d " " -f8,42,43 >> nav1.txt
        fi

        q2=0
        q2=`echo "${j}" | grep 'INFO - Number of Intervals Not Inserted:' | wc -l`
        if [$q2 -eq 1]; then
                echo "${j}" | tr -s " " | cut -d " " -f6-13 >> nav1.txt
        fi
done < nav11.txt

```

Output of nav1.txt:

```nohighlight
1-GH32X, CC, AMR,
Number of Intervals Not Inserted: 1 / 95
1-VIN74, CC, AMR,

```

Not sure if that is the stuff you want though.

---

<div class="post-metadata">

**Author:** ![Luka](https://community.unix.com/letter_avatar/luka/32/6_449c533f586939b6e5f246632a46421f.png) [@Luka](https://community.unix.com/u/Luka)\
**Post date:** [October 16, 2008, 7:13am UTC](https://community.unix.com/t/problem-in-while-loop-in-a-script/216758/9 "2008-10-16T07:13:15Z")

</div>

When I modified your script from the first post:

```nohighlight
#!/usr/bin/sh

while read j; do
        q1=0
        q1=`echo "$j" | grep 'INFO - LPBatch:' | wc -l`
        if [$q1 -eq 1]; then
                echo "$j" | tr -s " " | cut -d " " -f8,42,43 >> nav1.txt
        fi

        q2=0
        q2=`echo "$j" | grep 'INFO - Number of Intervals Not Inserted:' | wc -l`
        if [$q2 -eq 1]; then
                echo "$j" | tr -s " " | cut -d " " -f6-13 >> nav1.txt
        fi
done

```

Got this output:

```nohighlight
$ cat nav1.txt 
Number of Intervals Not Inserted: 1 / 95
1-GH32X, CC, AMR,
Number of Intervals Not Inserted: 1 / 95
1-VIN74, CC, AMR,

```

With no errors.

---

<div class="post-metadata">

**Author:** ![ali560045](https://community.unix.com/user_avatar/community.unix.com/ali560045/32/698_2.png) [@ali560045](https://community.unix.com/u/ali560045)\
**Post date:** [October 16, 2008, 7:24am UTC](https://community.unix.com/t/problem-in-while-loop-in-a-script/216758/10 "2008-10-16T07:24:07Z")

</div>

ok. thanks everybody for their precious help. i got the o/p with echo "${J}"

also i have little modified the script to add elif function. can any1 tell  
where i m going wrong below in the elif part

while read j  
do  
q1=0  
q2=0

q1=`echo "${j}" | grep 'INFO - LPBatch:' | wc -l`  
if [$q1 -eq 1]  
then  
echo "${j}" | tr -s " " | cut -d " " -f8,42,43 \>\> nav1.txt  
fi

q2=`echo "${j}" | grep 'INFO - Number of Intervals Not Inserted:' | wc -l`  
elif [$q2 -eq 1]  
then  
echo "${j}" | tr -s " " | cut -d " " -f6-13 \>\> nav1.txt  
fi

else  
echo "${j}" \>\> asma  
fi

done \< nav11.txt

---

<div class="post-metadata">

**Author:** ![Luka](https://community.unix.com/letter_avatar/luka/32/6_449c533f586939b6e5f246632a46421f.png) [@Luka](https://community.unix.com/u/Luka)\
**Post date:** [October 16, 2008, 7:26am UTC](https://community.unix.com/t/problem-in-while-loop-in-a-script/216758/11 "2008-10-16T07:26:52Z")

</div>

You finish the if sentence with fi and then start an elif ... ?  
It should go like this:  
if [something]; then  
something  
elif [somethingelse]; then  
somethingelse  
else  
whenallelsefails  
fi

---

<div class="post-metadata">

**Author:** ![zaxxon](https://community.unix.com/user_avatar/community.unix.com/zaxxon/32/660_2.png) [@zaxxon](https://community.unix.com/u/zaxxon)\
**Post date:** [October 16, 2008, 7:28am UTC](https://community.unix.com/t/problem-in-while-loop-in-a-script/216758/12 "2008-10-16T07:28:57Z")

</div>

Please use [code] and [/code] to display your code in those fancy eye friendly blue boxes. Also please describe what is not working now since you last changes.

To shorten it up, maybe you just post what's the wanted output.

---

<div class="post-metadata">

**Author:** ![ali560045](https://community.unix.com/user_avatar/community.unix.com/ali560045/32/698_2.png) [@ali560045](https://community.unix.com/u/ali560045)\
**Post date:** [October 16, 2008, 7:30am UTC](https://community.unix.com/t/problem-in-while-loop-in-a-script/216758/13 "2008-10-16T07:30:30Z")

</div>

my mistake a silly one .Actually if elif should be ended with one fi.

I really appreciate everbody's help. hatz off to all unix guru
