Aida E
Aida E

Reputation: 1100

problem with while loop

here is the script I wrote but it seems it has problem with while ! while suppose to compare the content of K with SDATE and while they are not equal go to the loop !

for d in \
 $(sed -nre 's/.*\[(..)\/(...)\/(....):(..:..:..) .*/\1 \2 \3 \4/p' thttpd.log | date +%s -f-);
do echo $d >s1; done

time=$(expr 60 \* 60 \* 24 \* 5)
EDATE=`tail -1 s1`
SDATE=$[$EDATE - $time]
time=$(expr 60 \* 60 \* 24 \* 5)
EDATE=`tail -1 s1`
SDATE=$[$EDATE - $time]
k=`tail -1 s1`
echo $k
echo $SDATE
while [$k -ne $SDATE](k and SDATE contain numbers)
 do
k=`tail -1 s1`
sed '1d' < s1 > tempfile
mv s1 s1.old
mv tempfile s1
echo $K| awk '{print strftime("%d/%m/%Y:%T",$1)}'|tee -a ass

done

Upvotes: 0

Views: 505

Answers (3)

Swiss
Swiss

Reputation: 5829

The problem is that you do not have spaces around [ or ]. Which causes BASH to parse the line incorrectly.

With the following line, BASH will attempt to run the program [$K, probably not what you are intending. while [$k -ne $SDATE]

What you need to have is the following:
while [ $k -ne $SDATE ]

Upvotes: 1

LavaSlider
LavaSlider

Reputation: 2504

Ah, shell programming is so touchy...

k=0
while [ $k != 4 ]; do echo $k; k=`expr $k + 1`; done

Works and prints 0, 1, 2, 3 on separate lines as expected and this essentially looks like what you have except for spaces, newlines, and sources of values. Maybe it is a string versus value problem but I did not think shell variables had types.

I would try stripping what you have back until it works then add back what you want it to do.

Upvotes: 0

Thomas
Thomas

Reputation: 181705

Try this:

while [[ $k != $SDATE ]]

Upvotes: 1

Related Questions