LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-12-2009, 06:09 PM   #1
akounga
LQ Newbie
 
Registered: Dec 2008
Posts: 16

Rep: Reputation: 0
Exclamation writting a script with a IF statement (Programming)


Hi all,

Can somebody help me; i'm trying to write a script with the IF statement which reads 5 grades, calculates and displays the lowest, highest and the final letter grade;
I've written the IF statement which reads 5 grades.
My problem is to write the script which will calculate and display the lowest, highest and the final letter grade.
This's what i've written so far:


gradeA=90
gradeB=80
gradeC=70
gradeD=60
gradeF=59

read -p "gradeA: " gradeA
read -p "gradeB: " gradeB
read -p "gradeC: " gradeC
read -p "gradeD: " gradeD
read -p "gradeF: " gradeF


if [[ $gradeA -ge 90 && $gradeA -lt 101 ]]
then
echo "gradeA"
else
echo "invalid"
fi

if [[ $gradeB -ge 80 && $gradeB -lt 90 ]]
then
echo "gradeB"
else
echo "invalid"
fi

if [[ $gradeC -ge 70 && $gradeC -lt 80 ]]
then
echo "gradeC"
else
echo "invalid"
fi

if [[ $gradeD -ge 60 && $gradeD -lt 70 ]]
then
echo "gradeD"
else
echo "invalid"
fi

if [[ $gradeF -ge 59 && $gradeF -lt 60 ]]
then
echo "gradeF"
else
echo "invalid"
fi
 
Old 06-13-2009, 03:08 AM   #2
vonbiber
Member
 
Registered: Apr 2009
Distribution: slackware 14.1 64-bit, slackware 14.2 64-bit, SystemRescueCD
Posts: 533

Rep: Reputation: 129Reputation: 129
Quote:
Originally Posted by akounga View Post
Hi all,

Can somebody help me; i'm trying to write a script with the IF statement which reads 5 grades, calculates and displays the lowest, highest and the final letter grade;
I've written the IF statement which reads 5 grades.
My problem is to write the script which will calculate and display the lowest, highest and the final letter grade.
This's what i've written so far:


gradeA=90
gradeB=80
gradeC=70
gradeD=60
gradeF=59

...
fi
after you've read gradeA, ..., gradeF
...

here's what you can do to get the lowest and highest
grade among $gradeA, ..., $gradeF

# print lowest, highest grade

lowest=$gradeA
highest=$gradeA

for grade in $gradeA $gradeB $gradeC $gradeD $gradeF
do
if [ $lowest -gt $grade ]; then
lowest=$grade
fi
if [ $highest -lt $grade ]; then
highest=$grade
fi
done

echo "highest grade: $highest"
echo "lowest grade: $lowest"
echo "out of these grades: $gradeA, $gradeB, $gradeC, $gradeD, $gradeF"
 
Old 06-13-2009, 03:38 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
homework?

use a case instead.
 
Old 06-13-2009, 08:47 AM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Code:
#!/bin/bash

f_grade() {
# Function to make looping through the grading process simpler
   l_grade=$1

   if [[ $l_grade -ge 0 && $l_grade -lt 101 ]]
   then
   # grade is valid!
      if [[ $l_grade -ge 90 ]]
      then
         echo "Grade A: $l_grade"
      elif [[ $l_grade -ge 80 ]]
      then
         echo "Grade B: $l_grade"
      else
         echo "Below B: $l_grade"
      fi
   else
      echo "Invalid grade: $l_grade!"
   fi
}

# Get grades from input
read -p "Please enter your grades: " g1 g2 g3 g4 g5

highest=$(echo "$g1
$g2
$g3
$g4
$g5"|sort -rn|head 1)

echo "Highest: $highest"
f_grade $highest

echo "out of:  $g1 $g2 $g3 $g4 $g5"

f_grade $g1
f_grade $g2
f_grade $g3
f_grade $g4
f_grade $g5
 
Old 06-14-2009, 03:55 PM   #5
akounga
LQ Newbie
 
Registered: Dec 2008
Posts: 16

Original Poster
Rep: Reputation: 0
Script Programming

Hi all,

Thanks to everybody who have responded to my question;
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
writting shell script and save in PDF file maryamabd Linux - Newbie 10 04-25-2009 10:34 AM
help writting an automated mail script? pachakutek Programming 6 07-20-2006 03:48 AM
Unix Shell Programming If Statement ']['HeBroken Programming 2 12-06-2004 03:21 PM
writting a backup shell script yenonn Slackware 2 03-18-2004 07:49 PM
Script and If Statement bspicer Linux - General 2 04-15-2002 06:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration