簡體   English   中英

直到循環執行bash腳本

[英]until loop for bash scripting

我想通過以下方式使用直到循環命令,直到條件為假時,使用直到直到腳本不會結束

#!/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%)

在這里它沒有結束日期..並生成日志文件

但我得到這個輸入

$ ./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命令出現問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM