Error of logica shell script

Asked

Viewed 131 times

0

I want to print on the screen a message according to the current time of the day but it always falls on Is, what would be the mistake? It’s 1:00 and I don’t understand what the mistake is.

inserir a descrição da imagem aqui

2 answers

2


The following comparison is unnecessary and will never be true:

elif [ "$HORA" -ge 12 -a "$HORA" -lt 06 ]
then
    echo "Boa Noite!";
else

Look at the solution:

#!/bin/bash

HORA=$(date +%H)

if [ "$HORA" -ge 6 -a "$HORA" -lt 12 ]
then
     echo "Bom dia!";
elif [ "$HORA" -ge 12 -a "$HORA" -lt 18 ]
then
    echo "Boa tarde!";
else
    echo "Boa noite!";
fi

1

try that, but I’m not so sure.

 #!/bin/bash

 clear
 HORA=$(date +%H)

 if [ "$HORA" -gt 05 -a "$HORA" -lt 12 ]
 then
     echo "Bom dia";

 elif [ "$HORA" -gt 11 -a "$HORA" -lt 18 ]
 then
     echo "Bom tarde";

 elif [ "$HORA" -gt 17 -o "$HORA" -lt 6 ]
 then
     echo "Bom noite";
 fi

I think the problem is right now, if you’re running this at night, there’s no way the time is over 18 and under 6

  • 1

    That code won’t work between 00:00hrs and 06:00hrs!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.