Search code examples
bashgreprangeoctoprint

How do i grep a range of data value output by a program in Bash Script


I am doing this script to check if my OctoPrint (3d-printer) is cooling down or not.

By using

#octocmd status 
temps:
    bed: actual=26.0, target=0.0, offset=0
    tool0: actual=54.9, target=55.0, offset=0
i will get a data like this.

i am able to check it is printing as i have done this in a bash script

/usr/local/bin/octocmd status | grep 'target=200.0, offset=0'
if [ $? == 0 ];  # if target 200; enter if
then
        echo "Printing in progress, Script will check back in 5 minutes"
        /usr/local/bin/octocmd status
        sleep 5m    

during cooling down i should see

tool0: actual=189.3(decreasing), target=0.0, offset=0

however i am stuck at trying at my ELSE function to check if it is cooling down. Lets say

actual= (**range from 40.0 to 180**), target=0.0, offset=0 

so i would like to humbly ask for help on how to detect any range of data from actual=XXX

this is my code currently:

    while [ 1 ]
do
/usr/local/bin/octocmd status | grep 'target=200.0, offset=0' &> /dev/null  # grab string $ remove error
if [ $? == 0 ];  # if target 200; enter if
then
        echo "Printing in progress, Script will check back in 5 minutes"
        /usr/local/bin/octocmd status
        sleep 5m                                # check every 5 minutes

elif  [ -z "/usr/local/bin/octocmd status |  /*CHECK IF PRINTER IS COOLING DOWN OR NOT, CHECK ACTUAL=30 ~ 180 */"' 2>&1 /dev/null ];
then
        if [ $? == 0 ];
        then
        #enter here if target is cooled with no print job
        /usr/local/bin/octocmd status

        fi

done

Solution

  • To complete anubhava answer here is how to include it in bash test:

    if [[ $(/usr/local/bin/octocmd status | awk -F 'actual=|[,(]'  '$2 >= 40 && $2 <= 180 {print 1}') ]]
      then 
        echo "OK"
      else 
        echo "Not OK"
    fi