Sample Header Ad - 728x90

How to specify AND / OR operators (conditions) for case statement?

7 votes
3 answers
4575 views
I have the following code.
-shell
read -p "Enter a word: " word

case $word in
  [aeiou]* | [AEIOU]*)
          echo "The word begins with a vowel." ;;
  [0-9]*)
          echo "The word begins with a digit." ;;
  *[0-9])
          echo "The word ends with a digit." ;;
  [aeiou]* && [AEIOU]* && *[0-9])
          echo "The word begins with vowel and ends with a digit." ;;
   ????)
          echo "You entered a four letter word." ;;
   *)
          echo "I don't know what you've entered," ;;
esac
When I run this:
Enter a word: apple123
case2.sh: line 10: syntax error near unexpected token `&&'
case2.sh: line 10: `  [aeiou]* && [AEIOU]* && *[0-9])'
It looks like case statement doesn't support AND operator, and also I believe the && operator in my above case statement logically incorrect. I understand that we can use if...else to check if the input starts with a vowel and digit. But I am curious if case has any builtin function like AND operator.
Asked by smc (621 rep)
Oct 28, 2019, 05:40 PM
Last activity: May 13, 2024, 04:19 PM