Register
It is currently Mon Nov 24, 2014 6:14 am

Just starting out


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Fri Oct 26, 2012 6:17 am   

Joined: Fri Oct 26, 2012 6:07 am
Posts: 4
Hello everyone I am new to Bash Script, and I am looking for some help. I am working on a program and need some pointers on how to go about it. This is what is requested:

Write a Bash script that calculates the area of geometric objects.

Your script should allow for the choice of rectangle, circle, trapezoid, and quit. (Using a menu for this is recommended.)

The rectangle option requires two arguments, the length and the width. The circle option requires one argument, the radius. The trapezoid option requires three arguments, the height and each of the parallel bases.

The script should use bc to output the area correct to three decimal places. Use 3.1416 as the value for Pi.

The following are the relevant area formulas.

Rectangle: A = length × width
Circle: A = Pi × radius2
Trapezoid: A = (base1 + base2)/2 × height

Like I mentioned I am new to the Bash Script, Any help is appreciated.


Top
 Profile  
 PostPosted: Fri Oct 26, 2012 8:07 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 567
hi,

`select' will help you build the menu
`read' will get input from user
`|' is to connect output of a command to the input of the next one.

do I have to rephrase your whole course?


Top
 Profile  
 PostPosted: Fri Oct 26, 2012 9:42 am   

Joined: Fri Oct 26, 2012 6:07 am
Posts: 4
I am working on the Code for the circle atm, I can post what I have later. I mainly some examples to help me through this.

Once I get home from work and school, I post what I have.


Top
 Profile  
 PostPosted: Wed Nov 07, 2012 8:47 am   

Joined: Fri Oct 26, 2012 6:07 am
Posts: 4
Hello again ok I figured out the first assignment, but I dont understand how to apply command line options to the code. Wondering if i could get a example if any one could help. I am suppose to be using -r,-c,-t for the shapes. I also need to use getopts to manage options. Here is my program:

PS3='Please enter your choice: '
options=("Calculate The Area Of A Rectangle" "Calculate The Area Of A Circle" "Calculate The Area Of A Trapezoid" "Quit")
select opt in "${options[@]}"
do
case $opt in
"Calculate The Area Of A Rectangle")
echo "Input lengh of a Rectangle: "
read length
echo "Input width of a Rectangle: "
read breadth
echo "Enter the Radius: "
read radius

area=$(echo $length \* $breadth | bc)
perimeter=$(echo 2 \* $length \* $breadth | bc)

echo "Area of the Rectangle:" $area
echo "Permimeter of the Rectangle:" $perimeter
;;
"Calculate The Area Of A Circle")
echo -n "Enter The radius of a circle: "
read r

#Forumula that calculates a Circle

area=$(echo "scale=2;3.1416 * ($r * $r)" | bc)

#Forumula that calculates a Circle

d=$(echo "scale=2;2 * $r"|bc)
circumference=$(echo "scale=2;3.1416 * $d"| bc)

echo "The Area Of The Circle Is $area"
echo "Circumference Of The Circle Is $circumference"
;;
"Calculate The Area Of A Trapezoid")
echo "Input the height of Trapezoid: "
read height
echo "Input Base1: "
read Base1
echo "Input Base2: "
read Base2

area=$(echo "($Base1 + $Base2)/2 * $height" | bc)

echo "Area Of The Trapezoid Is:" $area
;;
"Quit")
break
;;
*) echo invalid option;;
esac
done


Top
 Profile  
 PostPosted: Wed Nov 07, 2012 10:38 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 567
Code:
#!/bin/bash

options=("Calculate The Area Of A Rectangle" "Calculate The Area Of A Circle" "Calculate The Are a Of A Trapezoid" "Quit")

getopts 'rct' optName
case $optName in
   r) i="0"
   ;; 
   c) i="1"
   ;; 
   t) i="2"
   ;; 
   *) i="3"
esac
echo "${options[i]}"
more or less, this is easy.


think about using code tag when you post code.


Top
 Profile  
 PostPosted: Wed Nov 07, 2012 11:24 am   

Joined: Fri Oct 26, 2012 6:07 am
Posts: 4
thanks for the fast response I did figure this one out in the mean time I am on the last part atm



Code:
#!/bin/bash


while getopts :rctd: opt; do
  case $opt in
    r)
      echo "Input lengh of a Rectangle: "
read length
echo "Input width of a Rectangle: "
read breadth
echo "Enter the Radius: "
read radius

area=$(echo $length \* $breadth | bc)
perimeter=$(echo 2 \* $length \* $breadth | bc)

echo "Area of the Rectangle:" $area
echo "Permimeter of the Rectangle:" $perimeter

    ;;
    c)
      echo -n "Enter The radius of a circle: "
read r

#Forumula that calculates a Circle

area=$(echo "scale=2;3.14 * ($r * $r)" | bc)

#Forumula that calculates a Circle

d=$(echo "scale=2;2 * $r"|bc)
circumference=$(echo "scale=2;3.14 * $d"| bc)

echo "The Area Of The Circle Is $area"
echo "Circumference Of The Circle Is $circumference"
    ;;
    t)
      echo "Input the height of Trapezoid: "
read height
echo "Input Base1: "
read Base1
echo "Input Base2: "
read Base2

area=$(echo "($Base1 + $Base2)/2 * $height" | bc)

echo "Area Of The Trapezoid Is:" $area
    ;;
    d)

      if [[ $OPTARG = -* ]]; then
        ((OPTIND--))
        continue
      fi

      echo "(d) argument $OPTARG"
    ;;
    \?)
      echo "WTF!"
      exit 1
    ;;
  esac
done






Sorry about code post trying to get use to this......


Here is the last part I am working on, if anyone has Ideas

My script should also allow the option, -f. When this option is used in conjunction with -r, -c, or –t, each option takes one argument, the name of the input file. The input values would be read from the input file one line at a time and the areas printed to stdout. The script should terminate when a value of –1 is read. If the -f option is not used the script calculates only one value.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP