Lab 6


The following is a script that will ask the user the following:

  • To enter their name.
  • To enter their course code
  • To enter their grade percentage for the course.

The program will take the grade percentage and display a letter grade for the course depending on the grade percentage. The following is the percentage grades with letter grades:

Letter Grade          Numeric Average

A                            93 – 100%

A-                          90 – 93%

B+                         87 – 89%

B                           83 – 86%

B-                         80 – 82%

C+                        77 – 79%

C                          70 – 76%

D                          60 – 69%

F                          0 – 59%

After the user enters a percentage grade, the program will display the following:

“Hello ‘user name’. Your grade for ‘course code’  is ‘letter grade’.”

The program will use the users name and course code to display a message for the user to read. The percentage grade is then converted into a letter grade.

The following is the script used to make this happen.

#!/bin/bash

echo “Enter your name.”

read name

echo “Enter the course code.”

read course

echo “Enter your percentage grade.”

read percentage

if [ $percentage -le 100 ] && [ $percentage -ge 93 ]: then

echo “Hello $name. Your grade for $course is A.”

elif [ $percentage -le 92 ] && [ $percentage -ge 90 ]: then

echo “Hello $name. Your grade for $course is A-.”

elif [ $percentage -le 89 ] && [ $percentage -ge 87 ]: then

echo “Hello $name. Your grade for $course is B+.”

elif [ $percentage -le 86 ] && [ $percentage -ge 83 ]: then

echo “Hello $name. Your grade for $course is B.”

elif [ $percentage -le 82 ] && [ $percentage -ge 80 ]: then

echo “Hello $name. Your grade for $course is B-.”

elif [ $percentage -le 79 ] && [ $percentage -ge 77 ]: then

echo “Hello $name. Your grade for $course is C+.”

elif [ $percentage -le 76 ] && [ $percentage -ge 70 ]: then

echo “Hello $name. Your grade for $course is C.”

elif [ $percentage -le 69 ] && [ $percentage -ge 60 ]: then

echo “Hello $name. Your grade for $course is D.”

else

echo “Hello $name. Your grade for $course is F. You need to retake the class again.”

fi

HW GRADES

HW GRADES1

HW GRADES2


The following is a script that ask the user to enter a number. The script will than take that number and find the sum of all the numbers down to the number zero (0). For example, if the user enters the number 5, the script will do the following math (5+4+3+2+1+0) and then display the sum. The following is the script used to make this happen:

#!/bin/bash

echo “Please enter a number.”

read number

echo “The sum of $number to 0 is ”

for (( total=0; number>=0; –number )); do

total=$((total+number))

done

echo $total

 

As you can notice, the final message is displayed separately because if the line ‘echo “The sum of $number to 0 is” ‘, this line will take the term from number of the for loop. At the end of the for loop, number will be equal to -1. In order to keep the term number the same as the one the use entered, the message must be placed before the for loop. When you run the script, you will get the output message in two different line. Below are pictures when you run the script.

sum_of_numbers_1

sum_of_numbers_2

sum_of_numbers_3