在循环时中断并转到if / else

时间:2016-08-17 18:17:29

标签: bash shell if-statement while-loop break

我遇到问题是打破while循环并从while循环的开头开始。如果没有进入while循环检查日志文件,我想循环检查时间是否等于59秒。内部while循环应该只在日志文件中找到特定内容时执行某些操作。

编辑:新脚本。问题是while循环没有运行,也没有返回到在else循环中再次调用该函数的函数

脚本:

#!/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 个答案:

答案 0 :(得分:1)

你的尾巴显示最后0行。所以你的$line是空的。

  

tail -n0

你应该重新设定这个零值

<强>实施例

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

-n, - lines = K输出最后K行,而不是最后10行的默认值;或者,使用&#34; -n + K&#34;输出以Kth开头的行。

再次编辑:如果您想阅读完整的文件,请按我的方式进行操作

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

答案 1 :(得分: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处停止计数并转到第一个循环的开头。