2

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
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
Tsu Wei Quan
  • 335
  • 1
  • 5
  • 19
  • ??? I'm unsure I understood your question but: `if [[ $actual -gt 40 && $actual -lt 180 ]]` sounds to be your solution. After rereading I think I understood your problem, I'll write an answer – Tensibai Jan 16 '15 at 08:51
  • will it be able to 'actual=40.0' and so on up till 'actual=180.0' in that range? – Tsu Wei Quan Jan 16 '15 at 08:52
  • If i understood the question correctly you may want to use a while loop that ends when `$Actual` is within the given range –  Jan 16 '15 at 09:07

2 Answers2

3

You can use awk here with a custom field separator:

status | awk -F 'actual=|[,(]' '$2 >= 40 && $2 <= 180 {print "within range"}'
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • according to actual output `$2` will be `189.3(decreasing)` and can't be compared to 40 or 180 some more work has to be done on it for comparison to work (and as you're far better than me with awk, I ping to let you improve) – Tensibai Jan 16 '15 at 09:04
  • so i do have to assign $2 to actual? – Tsu Wei Quan Jan 16 '15 at 09:05
  • 3
    *Sigh* why did I forget to use a character class ... :) – Tensibai Jan 16 '15 at 09:13
1

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
Tensibai
  • 15,557
  • 1
  • 37
  • 57