In this article, we will solve VNSGU TYBCA Semester 5 Linux (UNIX) Practical – Set H using shell scripting.
This practical focuses on:
- User input
if-else- Arithmetic operations
- Percentage calculation
- Employee salary calculation
awkbc
📌 Practical Question
Write a shell script that computes the gross salary of an employee according to the following:
If Basic Salary is less than ₹50,000
- HRA = 20% of Basic Salary
- DA = 164% of Basic Salary
If Basic Salary is greater than ₹50,000
- HRA = 15% of Basic Salary
- DA = 136% of Basic Salary
Input Requirement
The basic salary should be entered interactively through the keyboard.
Gross Salary Formula
Gross Salary = Basic Salary + DA + HRA
🧠 Understanding the Problem
Let:
Basic Salary = B
If:
B < 50000
then:
HRA = 20% of B
DA = 164% of B
If:
B > 50000
then:
HRA = 15% of B
DA = 136% of B
Finally:
Gross Salary = B + HRA + DA
⚠️ Important Note About ₹50,000
The original question specifies:
Basic Salary < 50000
Basic Salary > 50000
It does not explicitly define what should happen when Basic Salary is exactly ₹50,000.
Therefore, in the solutions below, the 50000 case is handled separately.
🧮 Example Calculation
Suppose:
Basic Salary = ₹40,000
Since:
40000 < 50000
we use:
HRA = 20% of 40000
= 8000
DA = 164% of 40000
= 65600
Therefore:
Gross Salary = 40000 + 8000 + 65600
= ₹113600
✅ Solution 1: Using Basic Bash if-else
This is the simplest and most suitable solution for a university practical examination.
Shell Script
#!/bin/bash
echo "Enter Basic Salary:"
read basic
if [ "$basic" -lt 50000 ]
then
hra=$((basic * 20 / 100))
da=$((basic * 164 / 100))
elif [ "$basic" -gt 50000 ]
then
hra=$((basic * 15 / 100))
da=$((basic * 136 / 100))
else
echo "Basic salary of exactly 50000 is not defined in the question."
exit 1
fi
gross=$((basic + hra + da))
echo "-----------------------------"
echo "Basic Salary : $basic"
echo "HRA : $hra"
echo "DA : $da"
echo "Gross Salary : $gross"
echo "-----------------------------"
🔍 Step-by-Step Explanation
Step 1: Take Basic Salary
read basic
The salary entered by the user is stored in the variable basic.
Step 2: Check Salary Below ₹50,000
if [ "$basic" -lt 50000 ]
The -lt operator means:
Less than
So this condition checks:
basic < 50000
If the condition is true, HRA and DA are calculated using:
hra=$((basic * 20 / 100))
da=$((basic * 164 / 100))
Step 3: Check Salary Above ₹50,000
elif [ "$basic" -gt 50000 ]
The -gt operator means:
Greater than
So this condition checks:
basic > 50000
HRA and DA are then calculated using:
hra=$((basic * 15 / 100))
da=$((basic * 136 / 100))
Step 4: Calculate Gross Salary
gross=$((basic + hra + da))
The formula is:
Gross Salary = Basic Salary + HRA + DA
🖥️ Example Output – Basic Salary Less Than ₹50,000
Enter Basic Salary:
40000
-----------------------------
Basic Salary : 40000
HRA : 8000
DA : 65600
Gross Salary : 113600
-----------------------------
🖥️ Example Output – Basic Salary Greater Than ₹50,000
Suppose:
Basic Salary = 60000
Calculation:
HRA = 15% of 60000 = 9000
DA = 136% of 60000 = 81600
Output:
Enter Basic Salary:
60000
-----------------------------
Basic Salary : 60000
HRA : 9000
DA : 81600
Gross Salary : 150600
-----------------------------
❌ Example Output – Basic Salary Exactly ₹50,000
Enter Basic Salary:
50000
Basic salary of exactly 50000 is not defined in the question.
The script handles this case separately because the original question does not specify an HRA/DA rule for exactly ₹50,000.
⚠️ Limitation of Basic Bash Arithmetic
Bash arithmetic using:
$(( ... ))
works with integers.
For example:
echo $((5 / 2))
produces:
2
not:
2.5
If decimal salary values need to be supported, awk or bc can be used.
✅ Solution 2: Using awk
awk supports floating-point arithmetic, so it is useful when the basic salary can contain decimal values.
Shell Script
#!/bin/bash
echo "Enter Basic Salary:"
read basic
awk -v basic="$basic" '
BEGIN {
if (basic < 50000) {
hra = basic * 20 / 100
da = basic * 164 / 100
}
else if (basic > 50000) {
hra = basic * 15 / 100
da = basic * 136 / 100
}
else {
print "Basic salary of exactly 50000 is not defined in the question."
exit 1
}
gross = basic + hra + da
printf "-----------------------------\n"
printf "Basic Salary : %.2f\n", basic
printf "HRA : %.2f\n", hra
printf "DA : %.2f\n", da
printf "Gross Salary : %.2f\n", gross
printf "-----------------------------\n"
}'
🖥️ Example Output
Enter Basic Salary:
42500.50
-----------------------------
Basic Salary : 42500.50
HRA : 8500.10
DA : 69700.82
Gross Salary : 120701.42
-----------------------------
The %.2f format displays the values with two decimal places.
🔍 Why Use awk?
The main advantage is floating-point arithmetic.
For example:
awk 'BEGIN { printf "%.2f\n", 5 / 2 }'
Output:
2.50
So awk is useful when decimal values are required.
✅ Solution 3: Using bc
bc is another command-line calculator that can perform decimal calculations.
Shell Script
#!/bin/bash
echo "Enter Basic Salary:"
read basic
if (( $(echo "$basic < 50000" | bc -l) ))
then
hra=$(echo "scale=2; $basic * 20 / 100" | bc)
da=$(echo "scale=2; $basic * 164 / 100" | bc)
elif (( $(echo "$basic > 50000" | bc -l) ))
then
hra=$(echo "scale=2; $basic * 15 / 100" | bc)
da=$(echo "scale=2; $basic * 136 / 100" | bc)
else
echo "Basic salary of exactly 50000 is not defined in the question."
exit 1
fi
gross=$(echo "scale=2; $basic + $hra + $da" | bc)
echo "-----------------------------"
echo "Basic Salary : $basic"
echo "HRA : $hra"
echo "DA : $da"
echo "Gross Salary : $gross"
echo "-----------------------------"
🖥️ Example Output
Enter Basic Salary:
42500.50
-----------------------------
Basic Salary : 42500.50
HRA : 8500.10
DA : 69700.82
Gross Salary : 120701.42
-----------------------------
🔍 Understanding the bc Solution
This:
echo "$basic < 50000" | bc -l
checks the comparison using bc.
For example:
echo "42500.50 * 20 / 100" | bc
produces:
8500.10
The scale=2 option controls the number of decimal places in calculations.
📊 Comparing the Solutions
| Method | Decimal Support | Difficulty | External Command | Recommended |
|---|---|---|---|---|
Bash if-else
|
❌ Integer arithmetic | ⭐ Easy | No | ✅ Practical |
awk |
✅ Yes | ⭐⭐ | awk |
✅ Excellent |
bc |
✅ Yes | ⭐⭐⭐ | bc |
✅ Good |
Which Solution Should You Use?
For a normal university practical with integer salary input, Solution 1 is the easiest and most suitable.
If decimal salary values are required, use Solution 2 (awk) or Solution 3 (bc).
🧠 Important Operators Used
Less Than
-lt
Example:
[ "$basic" -lt 50000 ]
Greater Than
-gt
Example:
[ "$basic" -gt 50000 ]
Equal
-eq
Example:
[ "$basic" -eq 50000 ]
⚠️ Common Mistakes
Mistake 1: Using Assignment Instead of Comparison
Wrong:
if [ basic -lt 50000 ]
Correct:
if [ "$basic" -lt 50000 ]
Mistake 2: Incorrect Percentage Calculation
Wrong:
hra=$((basic * 20%))
Correct:
hra=$((basic * 20 / 100))
Mistake 3: Forgetting DA Is More Than 100%
The question specifies:
DA = 164%
for salary below ₹50,000 and:
DA = 136%
for salary above ₹50,000.
Do not accidentally write 16.4% or 13.6%.
Mistake 4: Forgetting the Gross Salary Formula
Remember:
Gross Salary = Basic Salary + HRA + DA
Mistake 5: Ignoring the ₹50,000 Case
The question gives:
< 50000
> 50000
but does not define:
= 50000
So the script handles that case separately.
🎯 Practical Exam Tips
Remember the basic structure:
if [ "$basic" -lt 50000 ]
then
# HRA 20%
# DA 164%
elif [ "$basic" -gt 50000 ]
then
# HRA 15%
# DA 136%
fi
Percentage calculation:
value=$((basic * percentage / 100))
Gross salary:
gross=$((basic + hra + da))
📝 Quick Revision
Salary below ₹50,000
hra=$((basic * 20 / 100))
da=$((basic * 164 / 100))
Salary above ₹50,000
hra=$((basic * 15 / 100))
da=$((basic * 136 / 100))
Gross Salary
gross=$((basic + hra + da))
Compare Less Than
[ "$basic" -lt 50000 ]
Compare Greater Than
[ "$basic" -gt 50000 ]
📚 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 Set H practical is a good exercise for understanding conditional statements, percentage calculations, user input, and arithmetic operations in shell scripting.
The basic logic is:
Read Basic Salary
↓
Check Salary
↓
Calculate HRA and DA
↓
Calculate Gross Salary
↓
Display Result
For a basic university practical, the Bash if-else solution is usually enough.
If decimal calculations are required, awk or bc can be used.
💬 What Do You Prefer?
For salary calculations in shell scripting, which approach do you prefer?
Basic Bash arithmetic, awk, or bc?
Share your approach in the comments! 🐧💻
📌 Tags
linux shell unix beginners
Top comments (0)