Home Linux Shell Script to check whether a student has passed or failed based on the marks

Shell Script to check whether a student has passed or failed based on the marks

by Anup Maurya
12 minutes read

In this article, we will discuss how to write a shell script to check whether a student has passed or failed based on the marks obtained in three subjects. The script will check whether any of the marks are less than 35, and if so, the student will be considered as failed. Let’s get started!

Before we dive into the code, let’s first understand what a shell script is. A shell script is a computer program designed to be run by the Unix/Linux shell, which is a command-line interpreter. Shell scripts are used to automate tasks, manipulate files, and perform other operations in a Unix/Linux environment.

Shell Script to check whether a student has passed or failed based on the marks

Now, let’s take a look at the shell script to check whether a student has passed or failed based on the marks obtained in three subjects.

#!/bin/bash

# Read in the marks obtained in three subjects
echo "Enter marks for subject 1: "
read subject1

echo "Enter marks for subject 2: "
read subject2

echo "Enter marks for subject 3: "
read subject3

# Check if the student has passed or failed
if [ $subject1 -lt 35 -o $subject2 -lt 35 -o $subject3 -lt 35 ]
then
    echo "Sorry, you have failed."
else
    echo "Congratulations, you have passed!"
fi

Let’s go through this script step by step:

  • The first line #!/bin/bash is called a shebang line. It tells the system that this file is a shell script and should be interpreted using the Bash shell.
  • The echo command is used to prompt the user to enter the marks obtained in three subjects.
  • The read command is used to read in the marks entered by the user and store them in the variables subject1, subject2, and subject3.
  • The if statement is used to check whether the student has passed or failed. The -lt operator is used to check whether any of the marks are less than 35. The -o operator is used to combine multiple conditions using an OR logic.
  • If any of the marks are less than 35, the script will print out a message saying that the student has failed. Otherwise, it will print out a message saying that the student has passed.

Now that we understand how this script works, let’s see how we can run it. To run this script, save it in a file with a .sh extension (e.g. pass_or_fail.sh), make the file executable with chmod +x pass_or_fail.sh, and then run the script with ./pass_or_fail.sh.

In conclusion, this shell script can be used to check whether a student has passed or failed based on the marks obtained in three subjects. It is a simple yet useful tool for students and teachers alike. With a little bit of knowledge of shell scripting, anyone can create scripts like this to automate tasks and simplify their workflow.

related posts

Leave a Comment