while循环的问题

发布于 2024-12-06 14:45:47 字数 611 浏览 0 评论 0原文

这是我写的脚本,但似乎 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愿与i 2024-12-13 14:45:47

问题是 [] 周围没有空格。这会导致 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 ]

雨后咖啡店 2024-12-13 14:45:47

试试这个:

while [[ $k != $SDATE ]]

Try this:

while [[ $k != $SDATE ]]
謸气贵蔟 2024-12-13 14:45:47

啊,shell 编程太敏感了...

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

按预期在单独的行上工作并打印 0、1、2、3,这基本上看起来像您所拥有的,除了空格、换行符和值源之外。也许这是一个字符串与值的问题,但我不认为 shell 变量有类型。

我会尝试剥离你所拥有的东西,直到它起作用,然后添加回你想要它做的事情。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文