Shell Scripting Tutorial for Beginners 13 - The case statement Example
#! /bin/bashecho -e "Enter some character : \c"read valuecase $value in [a-z] ) echo "User entered $value a to z" ;; [A-Z] ) echo "User entered $value A to Z" ;; [0-9] ) echo "User entered $value 0 to 9" ;; ? ) echo "User entered $value special character" ;; * ) echo "Unknown input" ;;esac
Output:
test@test$ ./hello.sh
Enter some character : fUser entered f a to ztest@test$ ./hello.sh
Enter some character : KUser entered K a to ztest@test$LANG=C
test@test$ ./hello.sh
Enter some character : KUser entered K A to Ztest@test$ ./hello.sh
Enter some character : 9User entered 9 0 to 9test@test$ ./hello.sh
Enter some character : 5User entered 5 0 to 9test@test$ ./hello.sh
Enter some character : &User entered & special charactertest@test$ ./hello.sh
Enter some character : sdsdsdsdUnknown inputtest@test$