【发布时间】:2015-05-13 15:25:24
【问题描述】:
我想按以下方式使用直到循环命令,使用直到循环,以便脚本在条件为假时不结束
#!/bin/bash
read -p " enter the App name : " app
file="/logs/$app/$app.log"
if ! [ -f "$file" ]; then
echo "$app.log not found, Please check correct log name in deployer"
exit
fi
format=$(head -1 $file | awk '{print $1,$2,$3}')
format1=$(tail -1 $file | awk '{print $1,$2,$3}')
read -p " Enter the Date in this Format --'$format' : " first
until grep -q "$first" "$file"
do
echo "Date not found"
read -p " Enter the Date in this Format --'$format' : " first
done
final_first=$first
echo "logs are present till this '$format1' date, use this date if you want logs from Provided to latest."
read -p "Enter the end date : " end
until grep -q "$end" "$file"
do
echo "Date not found"
read -p "Enter the end date : " end
done
final_end=$end
cd /logs/$app
sed -n " /$final_first/,/$final_end/ "p $file >$app.txt
zip $app.zip $app.txt
rm $app.txt
但我得到了这个输入
$ ./test
enter the App name : cspt
Enter the Date in this Format --'Sep 08 04:53:30' : er
logs are present till this 'at java.lang.Thread.run(Thread.java:662) ' date, use this date if you want logs from Provided to latest.
Enter the end date : Sep 08 04:53:30
adding: cspt.txt (deflated 99%)
这里没有结束日期..并生成日志文件
【问题讨论】:
-
空格很重要。在测试中,
[和]周围需要空格。话虽如此,您根本不想要[ ... ]。你想要grep的退出状态,所以你只想要until grep ...; do。 -
@EtanReisner 脚本再次失败,现在它不会进入下一步 $ ./test 输入应用名称:cspt 以这种格式输入日期 --'Sep 08 04:53:30':呃日志一直存在到此“at java.lang.Thread.run(Thread.java:662)”日期,如果您希望日志从提供到最新,请使用此日期。输入结束日期:Sep 08 04:53:30 添加:cspt.txt(放气 99%)在这里他现在没有询问结束日期
-
如果您仍然有问题,您可以使用更新的脚本和更新的错误来编辑帖子,以用新的问题“替换”问题(因为原来的问题本质上是一个错字)。 (不过,这在 SO 上通常不是有用的做法。)
-
@EtanReisner 请帮助我完成上述查询
-
@John Bollinger 请帮助我完成上述查询
标签: bash for-loop scripting infinite-loop