开发者

problem with while loop

here is the script I wrote but it seems it has pro开发者_JS百科blem 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


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 ]


Try this:

while [[ $k != $SDATE ]]


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜