while循环的问题
这是我写的脚本,但似乎 while 有问题! while 假设将 K 的内容与 SDATE 进行比较,当它们不相等时进入循环!
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是
[
或]
周围没有空格。这会导致 BASH 错误地解析该行。使用以下行,BASH 将尝试运行程序
[$K
,可能不是您想要的。while [$k -ne $SDATE]
您需要具备以下内容:
同时[$k -ne $SDATE]
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:
啊,shell 编程太敏感了...
按预期在单独的行上工作并打印 0、1、2、3,这基本上看起来像您所拥有的,除了空格、换行符和值源之外。也许这是一个字符串与值的问题,但我不认为 shell 变量有类型。
我会尝试剥离你所拥有的东西,直到它起作用,然后添加回你想要它做的事情。
Ah, shell programming is so touchy...
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.