Open In App

Bash Script to Check whether character entered is Upper, Lower, Digit, Consonant, or Vowel

Last Updated : 12 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In Bash scripting, it’s often necessary to analyze user input and determine the type of character entered. This information can be used for various purposes, like data validation, formatting, and logic control. This article demonstrates how to write a Bash script that checks whether a character entered by the user is uppercase, lowercase, digit, consonant, or vowel.

Understanding Character Types:

  • Uppercase: Characters between A and Z (ASCII codes 65 to 90).
  • Lowercase: Characters between a and z (ASCII codes 97 to 122).
  • Digit: Characters between 0 and 9 (ASCII codes 48 to 57).
  • Consonant: Any alphabetical character that is not a vowel (a, e, i, o, u).
  • Vowel: One of the letters a, e, i, o, or u.

Example:

Input: A

Output: Upper Case

Explanation: Here all the inputted data are in Upper Case and Vowel.

     

Input : p

Output : Lower Case

Explanation: Here all the inputted data are in Lower Case and Consonant.        

Input : 7

Output : Digit

Explanation: Here all the inputted data is a digit.   

Bash Script to Check whether character entered is Upper, Lower, Digit, Consonant, or Vowel

Here our task is to write a script to take input a variable, and it checks that the input string pr character is upper case, lower case, or digit input data. In the next loop, the character is checked for vowel or consonant. Each conditional statement is closed by the “fi” statement.

#!/bin/bash

# Input from user
echo "Enter the Character -"
read ch

# If condition to check character entered
# is upper case, lower case or digit
if [[ $ch == [A-Z] ]]
then
echo "Upper Case"
elif [[ $ch == [a-z] ]]
then 
echo "Lower Case"
elif [[ $ch == [0-9] ]]
then
echo "Digit"

fi

# If condition to check character 
# entered is consonant or vowel.
if [[ $ch == [AEIOUaeiou] ]]
then
echo "Vowel"
else
echo "Consonant"
fi

Explanation:

  • #!/bin/bash: This is the shebang line, indicating that the script should be run using the Bash shell located at /bin/bash.
  • echo "Enter the Character -": This line prints the message “Enter the Character -” to prompt the user to input a character.
  • read ch: This line reads the input from the user and stores it in the variable ch.
  • if [[ $ch == [A-Z] ]]: This is an if statement that checks if the character entered by the user is an uppercase letter (A-Z). The [[ ... ]] is a conditional construct that allows more flexible and extended tests.
  • echo "Upper Case": If the character entered is an uppercase letter, this line will be executed, and it will print “Upper Case” to the standard output.
  • elif [[ $ch == [a-z] ]]: This is an “else if” statement that checks if the character entered by the user is a lowercase letter (a-z).
  • echo "Lower Case": If the character entered is a lowercase letter, this line will be executed, and it will print “Lower Case” to the standard output.
  • elif [[ $ch == [0-9] ]]: This is another “else if” statement that checks if the character entered by the user is a digit (0-9).
  • echo "Digit": If the character entered is a digit, this line will be executed, and it will print “Digit” to the standard output.
  • if [[ $ch == [AEIOUaeiou] ]]: This is another if statement that checks if the character entered by the user is a vowel (either uppercase or lowercase).
  • echo "Vowel": If the character entered is a vowel, this line will be executed, and it will print “Vowel” to the standard output.
  • else: If the character entered is not a vowel (consonant), this line and the following line will be executed.
  • echo "Consonant": If the character entered is a consonant, this line will be executed, and it will print “Consonant” to the standard output.

Output:

Multiple examples

Multiple examples

Here,

`vim` = Used to create the script

`chmod +x` = Make our script executable.

`./test.sh`= use to execute our script (replace test.sh with your script name)

The following screenshot of the test case of the code is executed, it accepts the input data from the user. The entered character is been checked whether it is upper case, lower case, or digit data. Later, the character is checked to whether it is a vowel or consonant.

Conclusion

In this article we discussed how to know the type of character a user inputs using bash script. This article shows how to create a simple Bash script that checks if a character is uppercase, lowercase, digit, consonant, or vowel. The script uses ASCII codes for precise identification and logical conditions to determine the character’s type. Understanding and implementing this script can be helpful for various tasks like data validation in Bash programming.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads