In this article, we will solve VNSGU TYBCA Semester 5 Linux (UNIX) Practical – Set B using shell scripting.
We will learn how to:
- Count the number of vowels in a string
- Convert a string into uppercase
- Convert a string into lowercase
- Perform the same task using different Linux/UNIX approaches
📌 Practical Question
Write a script to count the number of vowels in the entered string and convert the string into upper case or lower case.
🧠 Concepts Used
This practical covers:
read- Variables
- String length
-
forloop -
ifcondition casetr- Bash parameter expansion
- String manipulation
✅ Solution 1: Using for Loop
This is one of the best approaches for understanding the logic behind vowel counting.
Shell Script
#!/bin/bash
echo "Enter a string:"
read str
count=0
for ((i=0; i<${#str}; i++))
do
ch=${str:$i:1}
case "$ch" in
a|e|i|o|u|A|E|I|O|U)
count=$((count + 1))
;;
esac
done
echo "Number of vowels: $count"
echo "Uppercase: ${str^^}"
echo "Lowercase: ${str,,}"
Example Output
Enter a string:
Hello Linux
Number of vowels: 4
Uppercase: HELLO LINUX
Lowercase: hello linux
🔍 Understanding the Script
Step 1: Take String Input
read str
The entered string is stored in the variable str.
Step 2: Initialize Counter
count=0
Initially, the number of vowels is zero.
Whenever a vowel is found, we increase the counter:
count=$((count + 1))
Step 3: Find String Length
${#str}
This gives the number of characters in the string.
For example:
Hello
has a length of:
5
Step 4: Get One Character
ch=${str:$i:1}
This extracts one character from the string.
For example, if:
str = Hello
then the loop extracts:
H
e
l
l
o
one by one.
Step 5: Check for Vowels
case "$ch" in
a|e|i|o|u|A|E|I|O|U)
count=$((count + 1))
;;
esac
If the character is one of:
a e i o u
A E I O U
the counter is increased.
Step 6: Convert to Uppercase
${str^^}
This converts the string into uppercase.
Example:
hello linux
becomes:
HELLO LINUX
Step 7: Convert to Lowercase
${str,,}
This converts the string into lowercase.
Example:
HELLO LINUX
becomes:
hello linux
Note:
${str^^}and${str,,}are Bash parameter-expansion features and are not available in very old shells.
✅ Solution 2: Using tr
Linux provides the tr command for character translation.
We can use it to convert the string into uppercase/lowercase and also count vowels.
Shell Script
#!/bin/bash
echo "Enter a string:"
read str
lower=$(echo "$str" | tr '[:upper:]' '[:lower:]')
count=$(echo "$lower" | tr -cd 'aeiou' | wc -c)
upper=$(echo "$str" | tr '[:lower:]' '[:upper:]')
echo "Number of vowels: $count"
echo "Uppercase: $upper"
echo "Lowercase: $lower"
Example Output
Enter a string:
Hello Linux
Number of vowels: 4
Uppercase: HELLO LINUX
Lowercase: hello linux
🔍 How tr Works
Convert to Lowercase
tr '[:upper:]' '[:lower:]'
Converts uppercase letters into lowercase.
Convert to Uppercase
tr '[:lower:]' '[:upper:]'
Converts lowercase letters into uppercase.
Remove Everything Except Vowels
tr -cd 'aeiou'
Here:
-
-c→ complement -
-d→ delete
So characters other than a, e, i, o, u are removed.
For:
Hello Linux
the result becomes approximately:
eoiu
Then:
wc -c
counts the remaining characters.
Therefore:
count=$(echo "$lower" | tr -cd 'aeiou' | wc -c)
gives the total number of vowels.
✅ Solution 3: Using grep
Another approach is to use grep to identify vowels.
Shell Script
#!/bin/bash
echo "Enter a string:"
read str
count=$(echo "$str" | grep -o -i '[aeiou]' | wc -l)
upper=$(echo "$str" | tr '[:lower:]' '[:upper:]')
lower=$(echo "$str" | tr '[:upper:]' '[:lower:]')
echo "Number of vowels: $count"
echo "Uppercase: $upper"
echo "Lowercase: $lower"
Example Output
Enter a string:
Welcome to Linux
Number of vowels: 6
Uppercase: WELCOME TO LINUX
Lowercase: welcome to linux
📊 Comparing the Solutions
| Method | Vowel Counting | Case Conversion | Difficulty |
|---|---|---|---|
for + case
|
✅ | Bash | ⭐ Easy |
tr + wc
|
✅ | tr |
⭐⭐ |
grep + wc
|
✅ | tr |
⭐⭐ |
Which solution should you use?
For a practical examination, I recommend the for loop + case solution if you want to demonstrate your shell scripting logic.
If the examiner expects Linux command usage, the tr solution is a very good option.
📝 Quick Revision
Count vowels using loop
case "$ch" in
a|e|i|o|u|A|E|I|O|U)
count=$((count + 1))
;;
esac
Uppercase
${str^^}
Lowercase
${str,,}
Uppercase using tr
echo "$str" | tr '[:lower:]' '[:upper:]'
Lowercase using tr
echo "$str" | tr '[:upper:]' '[:lower:]'
Count vowels using tr
echo "$str" | tr -cd 'aeiou' | wc -c
🎯 Practical Exam Tips
While writing this program in the practical exam, remember:
- Initialize the vowel counter with
0. - Check both uppercase and lowercase vowels.
- Use quotes around string variables when passing them to commands.
- Understand how
${#str}gives the string length. - Remember that
${str^^}and${str,,}are Bash-specific features. - If you want a more portable command-based approach, use
tr.
📚 Related Linux Practical Sets
This solution is part of the VNSGU TYBCA Sem 5 Linux (UNIX) Practical – OCT/Nov 2025 solution series.
- Set A – Simple Interest & Compound Interest
- Set B – Vowels Count & Case Conversion
- Set C – Palindrome String
- Set D – File Operations & Text Processing
- Set E – Display Lines with Validation
- Set F – Count Files & Directories
- Set G – Recently Modified Files
- Set H – Employee Gross Salary
🐧 Conclusion
The Vowels Count and Case Conversion practical is a useful shell scripting exercise for understanding strings, loops, conditions, Linux commands, and Bash parameter expansion.
Try writing each solution yourself instead of simply copying the code. Understanding the logic will make it much easier to solve similar questions during the practical examination.
Happy Learning & Best of Luck for your Linux Practical! 🐧💻
Top comments (0)