如何在Bash脚本中grep程序输出的一系列数据值

如何在Bash脚本中grep程序输出的一系列数据值,bash,grep,range,octoprint,Bash,Grep,Range,Octoprint,我执行此脚本是为了检查我的OctoPrint 3d打印机是否正在冷却 利用 #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. 我可以检查它是否正在打印,因为我已经在bash脚本中这样做了 /usr/local/bin/octocmd status | grep '

我执行此脚本是为了检查我的OctoPrint 3d打印机是否正在冷却

利用

#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.
我可以检查它是否正在打印,因为我已经在bash脚本中这样做了

/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    
在冷却过程中,我会看到

tool0: actual=189.3(decreasing), target=0.0, offset=0
然而,我一直在尝试我的ELSE函数来检查它是否正在冷却。 比方说

actual= (**range from 40.0 to 180**), target=0.0, offset=0 
因此,我想谦恭地寻求帮助,了解如何从actual=XXX检测任何范围的数据

这是我目前的代码:

    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
您可以在此处将awk与自定义字段分隔符一起使用:

status | awk -F 'actual=|[,(]' '$2 >= 40 && $2 <= 180 {print "within range"}'

要完成anubhava,这里的答案是如何将其包括在bash测试中:

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

??? 我不确定我是否理解你的问题,但是:如果[$actual-gt 40&$actual-lt 180]]听起来是你的解决方案。重读之后我想我理解了你的问题,我会写一个答案,它能在这个范围内“actual=40.0”等等直到“actual=180.0”吗?如果我正确理解了这个问题,你可能想使用一个while循环,当$actual在给定范围内时结束,根据实际输出$2将减少189.3,并且不能与40或180相比,因此需要做更多的工作与工作相比,由于你在awk方面比我强得多,我想让你提高。我必须给实际值分配2美元?唉,为什么我忘了使用角色类…: