2016-08-17 134 views
0

我有問題要打開while循環並從while循環的開始處開始。我想循環檢查連續檢查時間是否等於59秒,如果不去while循環檢查日誌文件。內部while循環只能在日誌文件中找到特定的內容時執行某些操作。Break while while循環並轉到if/else

編輯:新腳本。問題是,while循環不運行,不回去,在其他環

腳本再次調用該函數的功能:

#!/bin/bash  

counterSearch=0 
counterIssue=0 
counterPassed=0 
counterFailed=0 
counterSearchPassed=0 
counterSearchFailed=0 
counterIssuePassed=0 
counterIssueFailed=0 
counterTotal=0 
counterHourly=0 
counterAddHourly=0 
declare -a hourlyScan=('6' '0' '5' '0' '7' '2' '0' '13' '0' '18' '0' '0' '7' '0' '6' '0' '0' '1' '3' '0' '0' '0' '3' '0')  

function readLogFile {  

tail -n0 $logJira | \ 
while read line ; do  

    if echo "$line" | grep -e "/rest/api/2/search.*PASSED" 1>/dev/null 2>&1 ; then 
     echo "$date - Search and passed API action" >> $logIng 
     counterSearch=$((counterSearch+1)) 
     counterPassed=$((counterPassed+1)) 
     counterHourly=$((counterHourly+1)) 
     counterTotal=$((counterTotal+1)) 
     echo "$date - Total Passed API Authentication: $counterPassed" >> $logIng 
     echo "$date - Total search API actions: $counterSearch" >> $logIng 
     continue 2  

    elif echo "$line" | grep -e "/rest/api/2/search.*FAILED" 1>/dev/null 2>&1 ; then 
     echo "$date - Search and failed API action" >> $logIng 
     counterSearch=$((counterSearch+1)) 
     counterFailed=$((counterFailed+1)) 
     counterHourly=$((counterHourly+1)) 
     counterTotal=$((counterTotal+1)) 
     echo "$date - Total Failed API Authentication: $counterFailed" >> $logIng 
     echo "$date - Total search API actions: $counterSearch" >> $logIng 
     continue 2  

    elif echo "$line" | grep -e "/rest/api/2/issue.*PASSED" 1>/dev/null 2>&1 ; then 
     echo "$date - Issue and Passed API action" >> $logIng 
     counterIssue=$((counterIssue+1)) 
     counterPassed=$((counterPassed+1)) 
     counterHourly=$((counterHourly+1)) 
     counterTotal=$((counterTotal+1)) 
     echo "$date - Total Passed API Authentication: $counterPassed" >> $logIng 
     echo "$date - Total issue API actions: $counterIssue" >> $logIng 
     continue 2  

    elif echo "$line" | grep -e "/rest/api/2/issue.*FAILED" 1>/dev/null 2>&1 ; then 
     echo "$date -Issue and Failed API action" >> $logIng 
     counterIssue=$((counterIssue+1)) 
     counterFailed=$((counterFailed+1)) 
     counterHourly=$((counterHourly+1)) 
     counterTotal=$((counterTotal+1)) 
     echo "$date - Total Failed API Authentication: $counterFailed" >> $logIng 
     echo "$date - Total issue API actions: $counterIssue" >> $logIng 
     continue 2 
    fi 
done 
}  

function runner { 
while true; do 
currentMinute=$(date +%S) 
currentHour=$(date +%k) 
currentDay=$(date +%u) 
currentWeek=$(date +%W) 
    if [[ $currentMinute -eq 59 ]]; then 
    if [[ ${#hourlyScan[@]} -eq 24 ]]; then 
     unset hourlyScan[23] 
     hourlyScan=($counterHourly "${hourlyScan[@]}") 
     counterHourly=0  

     for i in "${!hourlyScan[@]}"; do 
      $cliScript --server $cliServer --user $cliUser --password $cliPass --action modifyPage --space "VEN" --title "API Usage Monitoring" \ 
      --findReplaceRegex "<tr><td>$i</td><td>(\d*)</td></tr>:<tr><td>$i</td><td>${hourlyScan[$i]}</td></tr>" 
     done 
    fi 
    else 
    readLogFile 
    fi 
done 
}  

runner 
+2

你能想出一個簡短的例子,演示相同的問題?目前尚不清楚這與您之前的問題有何不同。 – chepner

+0

不會把第二個while循環作爲「if -eq 59」工作的其他情況嗎?你有什麼嘗試? – lynxlynxlynx

+0

如果我將它放在else語句中,那麼它不適用於tail語句。我在外部while循環中嘗試了兩個while循環,但也沒有工作.. –

回答

1

你的尾巴顯示最後0線。所以你的$line是空的。

尾-n0

你應該remplace此零值

實例

> tail -n20 file.txt #display last 20 lines of file.txt 

-n,--lines = K輸出的最後K行,而不是最近10次的默認值;或者,使用「-n + K」輸出以Kth開頭的行。

再次編輯:如果您想閱讀完整的文件,有我的方式做到這一點

while read line 
do 
    echo -e "$line\n" 
done < file.txt 
+0

我已經添加了繼續到檢查日誌文件的while循環。但是上面的if語句永遠不會被執行。 –

+0

while循環繼續運行,沒關係。但是如果陳述也應該在第二秒是59秒的時候運行。 –

+0

我不確定要理解你最後一句話。無論如何,你是否嘗試使用'tail -n0 $ logJira'而不是'tail -n0 -F $ logJira'? –

0

我想你需要的是在內環一break。成像我的虛擬實例反映你想要做什麼:

#!/bin/bash 

while true; 
do 
    echo done 
    sleep 
    count=0 
    while true; 
    do 
    count=$(($count + 1)) 
    echo $count 
    if [ $count == "100" ]; then 
     break 
    fi 
    done 
    continue 
done 

這將停止在100計數並進入第一循環的開始。

+0

我試過這種方式,但它沒有奏效。只有for循環得到執行足夠悲傷。看看這個例子的開始帖子。 –

+0

然後我很確定問題是在別的地方。但不是因爲循環... –