Lab 6

The purpose of the script below is for a student to enter their name, course code, and numerical grade and the script will output there name, course code, and letter grade equivalent. The script uses an if statement in combination with elif (short for else if) to allow the program to branch for multiple situations. Like any script written within the shell the #!/bin/bash is implemented at the start of the script. Then the name, course, and numerical grade are read from the input of the user. The first statement in the if loop establishes a range for numbers greater than 93. If a user enters a number greater than 93 then that part of the loop is initialized. The rest of the script uses the elif statement which basically allows the script to branch or move on to the next situation if the previous range was false. Lastly, the else statement is used to cover all ranges not previously covered by the if and elif statements. At the end of this if statement fi is placed to allow the script to know that the if statement has finished.

#!/bin/bash
echo “Enter your name”
read name
echo “Enter Course Code”
read course
echo “Enter A numerical grade between 0-100″
read Grade
if [ $Grade -ge 93 ]; then
echo ” Hello” $name “Your grade for” $course “is an A”
elif [ $Grade -ge 90 -a $Grade -le 92 ]; then
echo ” Hello” $name “Your grade for” $course “is an A-”
elif [ $Grade -ge 87 -a $Grade -le 89 ]; then
echo ” Hello” $name “Your grade for” $course “is an B+”
elif [ $Grade -ge 83 -a $Grade -le 86 ]; then
echo ” Hello” $name “Your grade for” $course “is an B”
elif [ $Grade -ge 80 -a $Grade -le 82 ]; then
echo ” Hello” $name “Your grade for” $course “is an B-”
elif [ $Grade -ge 77 -a $Grade -le 79 ]; then
echo ” Hello” $name “Your grade for” $course “is an C+”
elif [ $Grade -ge 70 -a $Grade -le 76 ]; then
echo ” Hello” $name “Your grade for” $course “is an C-”
elif [ $Grade -ge 60 -a $Grade -le 69 ]; then
echo ” Hello” $name “Your grade for” $course “is an D”
else
echo ” Hello” $name “Your grade for” $course “is an F”

fi

Screen Shot 2014-11-30 at 12.31.06 AMScreen Shot 2014-11-30 at 12.33.00 AM

The script below asks the user to enter a positive integer of “num” and displays the sum of that number and all numbers before the entered value. For example, if the user enters 4 the output will be 10 as 4+3+2+1 = 10. The start of this script begins with the standard #!/bin/bash this is used to execute the script and indicate the scripts location. Next we ask the user to enter an integer and we read the value. Secondly, we define the variable sum and equal it to zero. Sum is equaled to zero because within the first loop there is no sum so it must start at zero. Next a for loop is created the variable “i” is set to 1 and is defined to be less then or equal to the “num” this is done to stop the loop when it gets to the entered integer. Additionally, i++ is written within the loop so that after every successful loop there will be an increment to “i” until it reaches the entered integer. Lastly, we use “do let” to tell the script that after every successful for loop is completed to let the sum equal the current i value added to the current sum. The sum is then displayed.

Example: User enters 4

For loop is true so i=1 sum= 0: 1+0=1

The loop initializes again as i is incremented and is still less then 4 so i=2 sum= 1: 1+2=3

The loop initializes again as i is incremented still less then 4 so i=3 sum= 3: 3+3=6

The loop initializes again as i is still less then or equal to 4 so i=4 sum= 6: 4+6=10

The Sum is then displayed as “Sum = 10”

 

#!/bin/bash
echo ” please enter a whole number”
read num
sum=0
for ((i=1; i<=num; i++));
do
let sum=$i+$sum

done
echo “Sum =” $sum

Screen Shot 2014-11-30 at 3.03.15 AM

Leave a Reply

Your email address will not be published. Required fields are marked *