Search code examples
stringbashnumbersevaluate

BASH Script - Check if consecutive numbers in a string are above a value


I am echoing some data from an Oracle DB cluster, via a bash script. Currently, my output into a variable in the script from SQLPlus is:

11/12 0 0 0 0 0 0 1 0 1 0 5 4 1 0 0 0 0 0 0 0 0 0 0 0

What I'd like to be able to do is evaluate that string of numbers, excluding the first one (the date), to see if any consecutive 6 of the numbers are above a certain value, lets say 10.

I only want the logic to return true if all 6 consecutive values were above "10".

So for example, if the output was:

11/12 0 0 8 10 5 1 1 0 8 10 25 40 6 2 0 0 0 0 0 0 0 0 0 0

The logic should return false/null/zero, anything I can handle negatively.

But if the string looked like this:

11/12 0 0 0 0 5 9 1 0 1 10 28 10 12 19 15 11 6 7 0 0 0 0

Then it would return true/1 etc..

Is there any bash component that I can make use of to do this? I've been stuck on this part for a while now.


Solution

  • For variety, here is a solution not depending on awk:

    #!/usr/bin/env bash
    
    contains() {
       local nums=$* count=0 threshold=10 limit=6 i
       for i in ${nums#* }; do
          if (( i >= threshold )); then
             (( ++count >= limit )) && return 0
          else
             count=0
          fi
       done
       return 1
    }
    
    output="11/12 0 0 0 0 5 9 1 0 1 10 28 10 12 19 15 11 6 7 0 0 0 0"
    if contains "$output"; then
       echo "Yaaay!"
    else
       echo "Noooo!"
    fi