休眠脚本的某些部分

休眠脚本的某些部分

我想暂停/睡眠 elif 部分,但保持其他 elif 部分正常工作。如果 1 个传感器关闭,我不希望整个脚本休眠。

#!/bin/sh

while true;  do
    valid=""


    scan=`./script`

    if [ "$scan" = "$valid" ]; then
        echo "Good value"

    else
        echo "Bad value"

    fi

    sleep 5
done

exit

因此,如果触发 1 个传感器,它会等待几秒钟,然后再为该 1 个传感器发送推送警报,但如果 1 个传感器延迟,它不会导致其他传感器停止发送警报。

我不想每次传感器关闭时都收到通知,我希望它们有延迟,这样它就不会一直向我发送警报。

答案1

#!/bin/bash
#again you must use bash... 
valid[0]=160650648 ; valid[1]=163686025
valid[2]=120806542 ; valid[3]=37206841
i=0

while sleep 5;  do
    scan=$(./RFSniffer)
    if [ "$scan" == "$oldscan" ]; then
    i=$((i+1))
    #if i is minor than 10 restart loop...
    #So a alert will not be displayed again after 50 seconds...
        if [ "$i" -lt "10" ]; then
            continue
        else 
            i=0
        fi
    fi

    case $scan in 
           ${valid[0]})
               echo "Good Read"
               echo "Your code is " $scan
               ./buzzer.sh &
               omxplayer -o local sleighbells.mp3
               curl -u "code": https://api.pushbullet.com/v2/pushes -d type=note -d title="Alert" -d body="DoorBell" &
            ;; 
            ${valid[1]})
                curl -u "code": https://api.pushbullet.com/v2/pushes -d type=note -d title="Alert" -d body="front sensor" &
            ;;
            ${valid[2]})
                curl -u "code": https://api.pushbullet.com/v2/pushes -d type=note -d title="Alert" -d body="Back door sensor" &
            ;;
            ${valid[3]})
                curl -u "code": https://api.pushbullet.com/v2/pushes -d type=note -d title="Alert" -d body="Second sensor" &
            ;;
            *)
               echo "BAD READ:  your code and the valid don't match"
               echo "Your correct valid code should be " $scan
            ;;
    esac

    oldscan=$scan
    scan=''
    #sleep 5 not needed sleep 5 is in while condition 

done

编辑:见评论..

答案2

我在解释“脚本应该独立传送读取的扫描但在发生后阻止每次扫描一分钟”时是否正确?

您的脚本有一些简化/优化的机会,我假设您正在使用bash(hoppla -bash标签已编辑掉...?),尽管 shebang 指示其他...因此,在为各个扫描实现一分钟暂停时,我还尝试抓住机会(用于echo我的系统上未安装的命令 - 如果对操作满意则删除)。尝试一下并随意适应:

VALID=(160650648 163686025 120806542 37206841)                          # define valid scans
BODY=(DoorBell "front sensor" "Back door sensor" "Second sensor")       # define body tests

while true
  do    scan=$(./RFSniffer)

        for IX in "${!VALID[@]}" "${#VALID[@]}"                         # 0 - 3 elements, and 4 to identify bad reads
          do    if [ "${scan:-FALSE}" = "${VALID[IX]}" ]                # compare 4 elements, the 5. tries to compare to "" empty
                  then  echo "Good Read"
                        echo "Your code is " $scan

                        if (( $SECONDS > ${LAST[IX]} + 60 ))            # test for one minute pause
                          then  if [ "$IX" = 0 ]                        # do this just for the "door bell"
                                  then  echo ./buzzer.sh
                                        echo omxplayer -o local sleighbells.mp3
                                fi

                                echo curl -u "code": https://api.pushbullet.com/v2/pushes -d type=note -d title="Alert" -d body="${BODY[IX]}" # &

                                LAST[IX]=$SECONDS                       # keep last scan occurrence
                          else  echo "pausing "                         
                        fi

                        break                                           # if good read - break out of the for loop to avoid the bad read msg
                fi

                if [ "$IX" = "${#VALID[@]}" ]                           # the fifth loop means no valid scan found

                  then  echo "BAD READ:  your code $scan and the valid ones don't match"
                        echo "Your correct valid code should be one of ${VALID[@]}"
                fi
          done

        sleep 5
  done

相关内容