Как правильно сообщить, если X & (Y <1 или Z> 2) - PullRequest
0 голосов
/ 06 июня 2019

Мне нужна помощь с приведенным ниже сценарием, все не получается, когда дело доходит до третьего элифа. Я пробовал оба <и -lt, оба провалились. Я не знаю, что делать дальше </p>

#!/bin/bash
currenttime=`date +%H%M`
morning="1800"
evening="2000"
host=127.0.0.1

while true; do
ping -c 1 -w 5 $host &> /dev/null

if [[ $? != 0 && ($currenttime > $evening || $currenttime < $morning) ]] #Ping down, and later than evening, or earlier than morning
then
        echo -e "Ping down, later than evening, earlier than morning"
elif [[ $? != 2 && ($currenttime > $evening || $currenttime < $morning) ]] #Ping up, and (later than evening, or earlier than morning)
then
        echo -e "Ping is up, later than evening, earlier than morning"

elif [[ $? != 0 && ($currenttime < $evening || $currenttime > $morning) ]] #Ping down, and (earlier than evening, or later than morning)
then
        echo -e "Ping is down, and it is earlier than evening or later than morning"

elif [[ $? != 2 && ($currenttime < $evening || $currenttime > $morning) ]] #Ping up,and (earlier than evening, or later than morning)
then
        echo -e "Ping is up and it is earlier than evening, or later than morning"

else
        echo "WTF?"
fi

done

Ответы [ 2 ]

0 голосов
/ 06 июня 2019

Для арифметических сравнений необходимо использовать -eq, -lt и т. Д. От man bash:

  arg1 OP arg2
          OP  is one of -eq, -ne, -lt, -le, -gt, or -ge.  These arithmetic
          binary operators return true if arg1 is equal to, not equal  to,
          less  than, less than or equal to, greater than, or greater than
          or equal to arg2, respectively.  Arg1 and arg2 may  be  positive
          or negative integers.

В настоящее время вы делаете $currenttime < $evening, что является сравнением строк.

0 голосов
/ 06 июня 2019
#!/bin/bash
currenttime=`date +%H%M`
morning="1800"
evening="2000"
host=127.0.0.1

while true; do
PINGOUTPUT=`ping -c 1 -w 5 $host &> /dev/null; echo $?`

if [[ $PINGOUTPUT != 0 && ($currenttime > $evening || $currenttime < $morning) ]] #Ping down, and later than evening, or earlier than morning
then
        echo -e "Ping down, later than evening, earlier than morning"
elif [[ $PINGOUTPUT != 2 && ($currenttime > $evening || $currenttime < $morning) ]] #Ping up, and (later than evening, or earlier than morning)
then
        echo -e "Ping is up, later than evening, earlier than morning"

elif [[ $PINGOUTPUT != 0 && ($currenttime < $evening || $currenttime > $morning) ]] #Ping down, and (earlier than evening, or later than morning)
then
        echo -e "Ping is down, and it is earlier than evening or later than morning"

elif [[ $PINGOUTPUT != 2 && ($currenttime < $evening || $currenttime > $morning) ]] #Ping up,and (earlier than evening, or later than morning)
then
        echo -e "Ping is up and it is earlier than evening, or later than morning"

else
        echo "WTF?"
fi

done
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...