I want to run a loop from 1 through 12, match the current loop number against pre-defined arrays then output which array is matched.
Each type of month gets an array.
lm #has 4,6,9,11
hm #has 1,3,5,7,8,10,12
feb #special case, if its not an hm or lm, its feb
Here is my current code :
#!/bin/bash
lm=()
lm+=(4)
lm+=(6)
lm+=(9)
lm+=(11)
hm=()
hm+=(1)
hm+=(3)
hm+=(5)
hm+=(7)
hm+=(8)
hm+=(10)
hm+=(12)
feb=()
feb+=(2)
ld=(1 2)
hd=(1 2)
fd=(1 2)
for m in {1..12}
do
echo "current month is $m"
if [[ " ${hm[*]} " == *"$m"* ]];
then
echo "high month"
elif [[ " ${lm[*]} " == *"$m"* ]];
then
echo "low month"
elif [[ " ${feb[*]} " = *"$m"* ]];
then
echo "feb"
else
echo "weird month input"
fi
done
Its output :
$ ./old2.sh
current month is 1
high month
current month is 2
high month
current month is 3
high month
current month is 4
low month
current month is 5
high month
current month is 6
low month
current month is 7
high month
current month is 8
high month
current month is 9
low month
current month is 10
high month
current month is 11
low month
current month is 12
high month
Look at 2, it is shown as high month (hm), ie, it has 31 days.
I have commented out the line that would add 12 or December in the array :
#!/bin/bash
lm=()
lm+=(4)
lm+=(6)
lm+=(9)
lm+=(11)
hm=()
hm+=(1)
hm+=(3)
hm+=(5)
hm+=(7)
hm+=(8)
hm+=(10)
#hm+=(12)
feb=()
feb+=(2)
ld=(1 2)
hd=(1 2)
fd=(1 2)
for m in {1..12}
do
echo "current month is $m"
if [[ " ${hm[*]} " == *"$m"* ]];
then
echo "high month"
elif [[ " ${lm[*]} " == *"$m"* ]];
then
echo "low month"
elif [[ " ${feb[*]} " = *"$m"* ]];
then
echo "feb"
else
echo "weird month input"
fi
done
New output :
$ ./old2.sh
current month is 1
high month
current month is 2
feb
current month is 3
high month
current month is 4
low month
current month is 5
high month
current month is 6
low month
current month is 7
high month
current month is 8
high month
current month is 9
low month
current month is 10
high month
current month is 11
low month
current month is 12
weird month input
Now the output is as expected.
Why is my script accepting 2 as hm, instead of feb, as it should?