In this Lab we learn how to do a two different script:
1) The first script will ask for user his/her name, course code and grade percentage, and the script will determine what letter grade will be assign to this student.
2) The second script will ask the user a integer, any integer, and the script will add all values between 0 and that integer.
NOTE: I use a raspberry pi using SSH, I download PuTTy on my laptop so i can see Linux terminal on my PC. I also use nano instead of vi because i found it more useful or at least best for my taste.
SCRIPT:
1- First Script:
In this script we first we declare shebang “#!/bin/bash” and declare the script as a “Bourne Again Shell”. Now we ask the user using “echo” command too display a text. Then i use command of “read” too read the input of the user and put the input on a variable, as you can see we use name, code, grade as variables that contains the user input.
Now we use ” if ” statements to determine which letter grade are right depending the user grade percentage. the first ” if ” statement is written like the first one on the code. The second one is written with ” elif ” because is if the first ” if ” is not true it skips to this one and check if the statement between the brackets is true , if is not true, it keeps going until is true. The last one is a ” else ” meaning the if non of the ” if ” or ” elif ” statement are not true it display the else statement.
Inside the ” if ” , ” elif ” and ” else ” are echo that will display the result of the student grade.
Inside the brackets is the condition per example: ” if [ $grade -ge 93 ]; then ” it means the if the grade you input is 93 or greater it will set the statement true and display the echo command under it.
COPY AND PASTE: PLEASE CHECK USING NANO EDITOR INSTEAD OF VI BECAUSE I USE NANO EVEN THOUGH I THINK IT WOULDN’T MATTER.
And the result is this:
2- Second Script:
For this script i found two ways to do it. The first way is using a for loop and the second one is using a equation like: [x(x+1)]/2 .
Like the first script we declare shebang and ” echo ” to display a text on the terminal then we read a variable.
Using the for loop way script we declare “y = 0″ and then make a for loop that count the variable ” i ” who is set to 0 and is counts 1 by 1 ” i=i+1″ until is greater than the number the the user integer input. Inside the for loop is this following statement ” y=$(($y + $i)) ” this mean that y will be y + i, and that will happen every loop until the for loop finish. And at the end we display echo to show the sum to the user.
NOTE: TYPO ERROR WHEN ITS SAID ” The sum of $number AND 0 : $y” its suppose to say “The sum of $number to 0 is: $y”
COPY AND PASTE: PLEASE CHECK USING NANO EDITOR INSTEAD OF VI BECAUSE I USE NANO EVEN THOUGH I THINK IT WOULDN’T MATTER.
And the output is like this:
Using [x(x+1)]/2 way script. First like all the script we show an echo that display a text than we read an integer from the user. We declare “two=2” then we declare “sum=$(($integer + 1))” this means that sum will the user input plus 1, then we declare “multi=$(($sum * $integer))” this means that multi will be the variable sum times the input user integer, then we declare “final=$(($multi/2))” this means the multi will be divided by two.
Finally we use an echo statement to display the result to the user.
COPY AND PASTE: PLEASE CHECK USING NANO EDITOR INSTEAD OF VI BECAUSE I USE NANO EVEN THOUGH I THINK IT WOULDN’T MATTER.
and the output is: