Danny
Danny

Reputation: 209

Break out while loop and go to if/else

I'm having problem to break out a while loop and start from the beginning of the while loop. I want to loop to check continuously if the time is equal to 59 seconds if not go to the while loop to check the logfile. The inner while loop should only do something when something specific is found in the logfile.

EDIT: New script. Problem is that the while loop doesn't run and don't go back to the function that calls the function again in the else loop

Script:

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

Upvotes: 0

Views: 561

Answers (2)

EA-Lille
EA-Lille

Reputation: 561

Your tail display last 0 lines. So your $line is empty.

tail -n0

You should remplace this zero value

Examples:

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

-n, --lines=K Output the last K lines, instead of the default of the last 10; alternatively, use "-n +K" to output lines starting with the Kth.

EDIT again : If you want to read the complete file, there my way to do it

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

Upvotes: 1

Emilio Pastor Mira
Emilio Pastor Mira

Reputation: 350

I think what you need is a break in the inner loop. Imaging that my dummy example reflects what you want to do:

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

This will stop counting at 100 and go to the beginning of the first loop.

Upvotes: 0

Related Questions