DEV Community

Fahad Vahora
Fahad Vahora

Posted on

Linux Practical Set H Solution – Employee Gross Salary

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
  • awk
  • bc

📌 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
Enter fullscreen mode Exit fullscreen mode

🧠 Understanding the Problem

Let:

Basic Salary = B
Enter fullscreen mode Exit fullscreen mode

If:

B < 50000
Enter fullscreen mode Exit fullscreen mode

then:

HRA = 20% of B
DA  = 164% of B
Enter fullscreen mode Exit fullscreen mode

If:

B > 50000
Enter fullscreen mode Exit fullscreen mode

then:

HRA = 15% of B
DA  = 136% of B
Enter fullscreen mode Exit fullscreen mode

Finally:

Gross Salary = B + HRA + DA
Enter fullscreen mode Exit fullscreen mode

⚠️ Important Note About ₹50,000

The original question specifies:

Basic Salary < 50000
Basic Salary > 50000
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Since:

40000 < 50000
Enter fullscreen mode Exit fullscreen mode

we use:

HRA = 20% of 40000
    = 8000

DA = 164% of 40000
   = 65600
Enter fullscreen mode Exit fullscreen mode

Therefore:

Gross Salary = 40000 + 8000 + 65600
             = ₹113600
Enter fullscreen mode Exit fullscreen mode

✅ 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 "-----------------------------"
Enter fullscreen mode Exit fullscreen mode

🔍 Step-by-Step Explanation

Step 1: Take Basic Salary

read basic
Enter fullscreen mode Exit fullscreen mode

The salary entered by the user is stored in the variable basic.


Step 2: Check Salary Below ₹50,000

if [ "$basic" -lt 50000 ]
Enter fullscreen mode Exit fullscreen mode

The -lt operator means:

Less than

So this condition checks:

basic < 50000
Enter fullscreen mode Exit fullscreen mode

If the condition is true, HRA and DA are calculated using:

hra=$((basic * 20 / 100))
da=$((basic * 164 / 100))
Enter fullscreen mode Exit fullscreen mode

Step 3: Check Salary Above ₹50,000

elif [ "$basic" -gt 50000 ]
Enter fullscreen mode Exit fullscreen mode

The -gt operator means:

Greater than

So this condition checks:

basic > 50000
Enter fullscreen mode Exit fullscreen mode

HRA and DA are then calculated using:

hra=$((basic * 15 / 100))
da=$((basic * 136 / 100))
Enter fullscreen mode Exit fullscreen mode

Step 4: Calculate Gross Salary

gross=$((basic + hra + da))
Enter fullscreen mode Exit fullscreen mode

The formula is:

Gross Salary = Basic Salary + HRA + DA
Enter fullscreen mode Exit fullscreen mode

🖥️ Example Output – Basic Salary Less Than ₹50,000

Enter Basic Salary:
40000
-----------------------------
Basic Salary : 40000
HRA          : 8000
DA           : 65600
Gross Salary : 113600
-----------------------------
Enter fullscreen mode Exit fullscreen mode

🖥️ Example Output – Basic Salary Greater Than ₹50,000

Suppose:

Basic Salary = 60000
Enter fullscreen mode Exit fullscreen mode

Calculation:

HRA = 15% of 60000 = 9000
DA  = 136% of 60000 = 81600
Enter fullscreen mode Exit fullscreen mode

Output:

Enter Basic Salary:
60000
-----------------------------
Basic Salary : 60000
HRA          : 9000
DA           : 81600
Gross Salary : 150600
-----------------------------
Enter fullscreen mode Exit fullscreen mode

❌ Example Output – Basic Salary Exactly ₹50,000

Enter Basic Salary:
50000

Basic salary of exactly 50000 is not defined in the question.
Enter fullscreen mode Exit fullscreen mode

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:

$(( ... ))
Enter fullscreen mode Exit fullscreen mode

works with integers.

For example:

echo $((5 / 2))
Enter fullscreen mode Exit fullscreen mode

produces:

2
Enter fullscreen mode Exit fullscreen mode

not:

2.5
Enter fullscreen mode Exit fullscreen mode

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"
}'
Enter fullscreen mode Exit fullscreen mode

🖥️ Example Output

Enter Basic Salary:
42500.50

-----------------------------
Basic Salary : 42500.50
HRA          : 8500.10
DA           : 69700.82
Gross Salary : 120701.42
-----------------------------
Enter fullscreen mode Exit fullscreen mode

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 }'
Enter fullscreen mode Exit fullscreen mode

Output:

2.50
Enter fullscreen mode Exit fullscreen mode

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 "-----------------------------"
Enter fullscreen mode Exit fullscreen mode

🖥️ Example Output

Enter Basic Salary:
42500.50

-----------------------------
Basic Salary : 42500.50
HRA          : 8500.10
DA           : 69700.82
Gross Salary : 120701.42
-----------------------------
Enter fullscreen mode Exit fullscreen mode

🔍 Understanding the bc Solution

This:

echo "$basic < 50000" | bc -l
Enter fullscreen mode Exit fullscreen mode

checks the comparison using bc.

For example:

echo "42500.50 * 20 / 100" | bc
Enter fullscreen mode Exit fullscreen mode

produces:

8500.10
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Example:

[ "$basic" -lt 50000 ]
Enter fullscreen mode Exit fullscreen mode

Greater Than

-gt
Enter fullscreen mode Exit fullscreen mode

Example:

[ "$basic" -gt 50000 ]
Enter fullscreen mode Exit fullscreen mode

Equal

-eq
Enter fullscreen mode Exit fullscreen mode

Example:

[ "$basic" -eq 50000 ]
Enter fullscreen mode Exit fullscreen mode

⚠️ Common Mistakes

Mistake 1: Using Assignment Instead of Comparison

Wrong:

if [ basic -lt 50000 ]
Enter fullscreen mode Exit fullscreen mode

Correct:

if [ "$basic" -lt 50000 ]
Enter fullscreen mode Exit fullscreen mode

Mistake 2: Incorrect Percentage Calculation

Wrong:

hra=$((basic * 20%))
Enter fullscreen mode Exit fullscreen mode

Correct:

hra=$((basic * 20 / 100))
Enter fullscreen mode Exit fullscreen mode

Mistake 3: Forgetting DA Is More Than 100%

The question specifies:

DA = 164%
Enter fullscreen mode Exit fullscreen mode

for salary below ₹50,000 and:

DA = 136%
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Mistake 5: Ignoring the ₹50,000 Case

The question gives:

< 50000
> 50000
Enter fullscreen mode Exit fullscreen mode

but does not define:

= 50000
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Percentage calculation:

value=$((basic * percentage / 100))
Enter fullscreen mode Exit fullscreen mode

Gross salary:

gross=$((basic + hra + da))
Enter fullscreen mode Exit fullscreen mode

📝 Quick Revision

Salary below ₹50,000

hra=$((basic * 20 / 100))
da=$((basic * 164 / 100))
Enter fullscreen mode Exit fullscreen mode

Salary above ₹50,000

hra=$((basic * 15 / 100))
da=$((basic * 136 / 100))
Enter fullscreen mode Exit fullscreen mode

Gross Salary

gross=$((basic + hra + da))
Enter fullscreen mode Exit fullscreen mode

Compare Less Than

[ "$basic" -lt 50000 ]
Enter fullscreen mode Exit fullscreen mode

Compare Greater Than

[ "$basic" -gt 50000 ]
Enter fullscreen mode Exit fullscreen mode

📚 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
Enter fullscreen mode Exit fullscreen mode

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)