[]
indica intervalos de caracteres:
[10-12] significa dígitos 1 2 e o intervalo entre dígitos 0-1 - isso corresponderá a um único dígito no intervalo 0-2
.
Use comparações simples com if-elif-else-fi
:
if [ "$tmp" -ge 0 ] && [ "$tmp" -le 5 ]; then
echo "<0,5>"
elif [ "$tmp" -ge 6 ] && [ "$tmp" -le 9 ]; then
echo "<6,9>"
#...
else
#...
fi
(Ou você pode iterar sobre uma matriz de limites de intervalo se quiser cada intervalo, mas também pode codificá-lo nesse caso - como você está tentando fazer).
Editar: versão da matriz solicitada:
FuzzyTime(){
local needle=$1 #needle is $1
: ${needle:=$( date +%H )} #if no needle is empty, set it to "$(date +%H)
local times=( 0 6 10 13 19 22 24 0 )
local strings=(
"why don't you go to bed"
"I see your very eager to start the day"
"and a very good day too you"
"Good Afternoon"
"Good Evening"
"it is getting late, it's time to party or go to bed"
"guess the planet your on has more than a 24 hour rotation"
)
local b=0
# length(times) - 2 == index of the penultimate element
local B="$((${#times[@]}-2))"
for((; b<B; b++)); do
if ((needle >= times[b] && needle < times[b+1])); then break; fi
done
echo "${strings[$b]}"
}
FuzzyTime "$1"
teste :
$ for t in {0..27}; do FuzzyTime "$t"; done
0 -- why don't you go to bed
1 -- why don't you go to bed
2 -- why don't you go to bed
3 -- why don't you go to bed
4 -- why don't you go to bed
5 -- why don't you go to bed
6 -- I see your very eager to start the day
7 -- I see your very eager to start the day
8 -- I see your very eager to start the day
9 -- I see your very eager to start the day
10 -- and a very good day too you
11 -- and a very good day too you
12 -- and a very good day too you
13 -- Good Afternoon
14 -- Good Afternoon
15 -- Good Afternoon
16 -- Good Afternoon
17 -- Good Afternoon
18 -- Good Afternoon
19 -- Good Evening
20 -- Good Evening
21 -- Good Evening
22 -- it is getting late, it's time to party or go to bed
23 -- it is getting late, it's time to party or go to bed
24 -- guess the planet your on has more than a 24 hour rotation
25 -- guess the planet your on has more than a 24 hour rotation
26 -- guess the planet your on has more than a 24 hour rotation
27 -- guess the planet your on has more than a 24 hour rotation