I am attempting to write a bash script that takes input from the keyboard and displays text depending on what number was entered:
#!/bin/bash
read -p "Enter your age: " AGE
case $AGE in
[1-12]*)
echo "You are just a child." ;;
[13-19]*)
echo "You are just a teenager." ;;
[20-29]*)
echo "You are a young adult." ;;
[30-39]*)
echo "You are a moderate age adult." ;;
*)
echo "You are really old."
esac
When you enter numbers like 3 or 4, it doesn't fall in the first range. It falls withing the 30-39 and the default.
Where am I messing up?
[20-29]*
form regular expressions to denote character ranges, not number ranges. So you may write[0-5]
to mean the digits 0,1,2,3,4,5. But [20-29] is interpreted as the digit 2, the digits 0-2 (which overlaps in the digit 2) and the digit 9. The asterix allows multiple such digits in series. – user unknown Feb 22 '23 at 15:39case
statements in the shell use shell patterns, not regexes. The brackets are similar, but the asterisk means "any amount of any characters" (and not "any amount of the previous atom"). – ilkkachu Feb 22 '23 at 15:41ls -l *.txt
) take shell patterns,grep
takes regexes. AWK uses (extended) regexes,find -name
takes patterns. And the syntaxes are different, so mistaking one for the other will just make it not work. – ilkkachu Feb 23 '23 at 13:35