LAB 6

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.

 

GRADE CODE

 

COPY AND PASTE:  PLEASE CHECK USING NANO EDITOR INSTEAD OF VI BECAUSE I USE NANO EVEN THOUGH I THINK IT WOULDN’T MATTER.

#!/bin/bash
echo “Please enter your name:”
read name
echo “Please enter your course code:”
read code
echo “Please enter your grade percentage:”
read grade
if [ $grade -ge 93 ]; then
   echo ” $name in  $code you got a grade of A”
elif [ $grade -ge 90 ]; then
   echo ” $name in $code you got a grade of -A”
elif [ $grade -ge 87 ]; then
   echo ” $name in $code you got a grade of B+”
elif [ $grade -ge 83 ]; then
   echo “$name in $code  you got a grade of B”
elif [ $grade -ge 80 ]; then
   echo “$name in $code  you got a grade of B-“
elif [ $grade -ge  77 ]; then
   echo “$name in $code you got a grade of C+”
elif [ $grade -ge 70 ]; then
   echo “$name in $code you got a grade of C”
elif [ $grade -ge 60 ]; then
   echo “$name in $code you got a grade of D”
else
   echo “$name in $code you got a grade of F”
fi

And the result is this:

GRADE RUNNING

 

 

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.

SUM BY FOR LOOP CODE AND NEGATIVE

 

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.

#!/bin/bash
y=0
echo “Please enter a interger number”
read number
if [ $number -ge 0 ]
then
       for ((i=0; i<=$number; i=i+1))
        do
                y=$(($y + $i))
        done
else
       for ((i=$number; i<=0; i=i+1))
       do
                y=$(($y + $i))
       done
fi
echo “The sum of $number and 0 is : $y”

And the output is like this:

 

SUM BY FOR LOOP OUTPUT AND NEGATIVE

 

 

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.

 

SUM BY FORMULA CODE ALSO NEGATIVE

COPY AND PASTE:  PLEASE CHECK USING NANO EDITOR INSTEAD OF VI BECAUSE I USE NANO EVEN THOUGH I THINK IT WOULDN’T MATTER.

#! /bin/bash
echo “Please enter an interger”
read interger
if [ $interger -ge 0 ]; then
{ sum=$(($interger + 1))
 multi=$(($sum * $interger))
 final=$(($multi / 2))
 echo “The sum of all interger between 0 and  $interger: $final”
}
else
{ sum=$(($interger -1))
 multi=$(($sum * $interger))
 final=$(($multi / 2))
 f=$(($final * -1))
 echo “The sum of all integer between $interger and 0 : $f “
}
fi

and the output is:

 

SUM BY FORMULA OUTPUT ALSO NEGATIVE

 

Leave a Reply

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