Lab 6

Part I

Objective:

  • Create one script that will ask the user the following data:
    1. Name
    2. Course Code
    3. Grade Percentage
  • After getting the data, use the grading scale in the syllabus, and display the appropriate message

Example: “Hello John Doe. Your grade for EMT 2390L is A”

Script Code

Scriptpart1Code

Script Running

scriptpart1coderunning

For part 1 of the lab we created a script that asks the user to enter their name, course and numerical grade. Once the name, course and numerical grade is inputted, the script will then output the letter grade that matches the numerical grade given from the syllabus in a message. In the screenshot above we can see the the after entering name, course and numerical grade the script then displays the message “Hello Andrew, your grade for EMT 2390L is A-.” “#!/bin/bash” on the first line, indicates that the script should always be run with bash, rather than another shell. The command “echo” is used to display a message across the screen and “read” is used to read a line from standard input. In the script the “while” loop is used for when the user enters a number greater than 100 it will prompt the user to enter a valid number. Also used in the script is “elif” known as the else if statement. It allows the shell to make the correct decision out of several conditions. “fi” is used in the script to end the if statement and will execute the rest of a script. In the script we use “-gt” for greater then and “-ge” for greater than or equal to.

Part II

Objective:

  • Create another script that will ask the user for an integer n, and displays the sum of the first n numbers

Example: input = 4; output = 10

Script Code

part2code

Script Running

part2coderunning

For part 2 of the lab we created another script that asks the user for an integer n and display the sum of the first n numbers.  In the screenshot above we can see the script asks the user to enter a number. The number 6 is inputted into the script resulting the output to 21. The script reads “The sum of 0 to 6 is 21.” This means 0 + 1 + 2 + 3 + 4 + 5 + 6 = 21. “#!/bin/bash” on the first line, indicates that the script should always be run with bash, rather than another shell. The command “echo” is used to display a message across the screen and “read” is used to read a line from standard input. The “for” loop is used in the script. It is the first of the three shell looping constructs. This loop allows for specification of a list of values. A list of commands is executed for each value in the list.

Leave a Reply

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