【问题标题】:until loop for bash scripting用于 bash 脚本的直到循环
【发布时间】: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


【解决方案1】:

但我得到了这个输入

$ ./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%)

这里没有结束日期..并生成日志文件

相反,它看起来确实在提示并读取结束日期(在示例中以Sep 08 04:53:30 给出)。您的意思是说您希望脚本拒绝将er 作为开始日期?

也许脚本可以更具辨别力,但我认为没有理由认为它所做的事情与你告诉它做的事情不同。您或多或少地接受出现在文件中任何位置的任何字符串作为开始“日期”。认为字符串“er”会出现在冗长的日志文件中的某个地方并非没有道理。

另外请注意,如果任一给定的“日期”包含正则表达式元字符,您的脚本可能会出现异常行为。这样的日期可能会在您不期望的地方匹配。

此外,包含斜杠 (/) 字符的日期会导致您的 sed 命令出现问题。

【讨论】:

    猜你喜欢
    • 2017-10-05
    • 1970-01-01
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    相关资源
    最近更新 更多