filter="yes"
phonenumbers=(123 456 789 987 654 321)
echo "checking inbox now..."
myphonenumber=(7892)
if [[ "$filter" == "yes" ]]; then
case "${phonenumbers[@]}" in
*"$myphonenumber"*) echo "filter is on, phone number matches"
;;
!="$myphonenumber") echo "filter is on but the phone number doesn't match"
;;
esac
fi
if [[ "$filter" == "no" ]]; then
echo "filter off"
fi
I try running that script but it doesn't work, how should I display the filter is on but the phone number doesn't match
part? I'm still learning, I know I can do it with if else
statement but I wonder if I can do it with case
too.
Solved with this *)
so the whole code is:
filter="yes"
phonenumbers=(123 456 789 987 654 321)
echo "checking inbox now..."
myphonenumber=(7892)
if [[ "$filter" == "yes" ]]; then
case "${phonenumbers[@]}" in
*"$myphonenumber"*) echo "filter is on, phone number matches" ;;
*) echo "filter is on but the phone number doesn't match" ;;
esac
fi
if [[ "$filter" == "no" ]]; then
echo "filter off"
fi
If you are looking for something similar to a default
statement of a switch
in a C-like language, the following should handle this. Observe the *)
clause.
For a bit of reference, http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_03.html
filter="yes"
phonenumbers=(123 456 789 987 654 321)
echo "checking inbox now..."
myphonenumber=(7892)
if [[ "$filter" == "yes" ]]; then
case "${phonenumbers[@]}" in
*"$myphonenumber"*) echo "filter is on, phone number matches"
;;
*) echo "filter is on but the phone number doesn't match"
;;
esac
fi
if [[ "$filter" == "no" ]]; then
echo "filter off"
fi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With