CS 9E - Assignment 4.1

BASH Scripting: BASH Calculator

Background

Overview

This assignment will introduce you to BASH scripting and the process of creating customized scripts and functions using BASH and external UNIX commands.

Reading

Advanced Bash Scripting Guide: Sections 1-8, 23

Basic Calculator (bc) Man Page: The 'MATH LIBRARY' and 'EXPRESSIONS' sections will be especially helpful

Note

The version of bc installed on the instructional systems in /usr/bin/bc is not GNU bc. GNU bc has been installed at ~cs9e-1/bin/arch/sun4u/bc which should be in your path. If you are having trouble with bc behaving unexpectidly, make sure you are using the right one:

$ bc --version                                                               
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.

Basic Calculator Primer

Unlike most programming languages, BASH does not have builtin floating point math (it does have builtin integer math, however). This is a feature of BASH rather than a shortcoming because it allows BASH to be easily ported to systems which lack floating point processors. Because of this, you need to figure out a way to handle the floating point math required for this assignment. The simplest way to do this is through the external command 'bc'. Make sure you call it using the '-l' command line option so it loads the trig math library. See the manual page reference in the readings above for more information. You can use bc interactively to explore its syntax and usage, but in your script, you will want to pipe in expressions.

NOTE: You are not required to use bc. If you can find another unix tool which allows you to complete this assignment, you are more than welcome to use it instead. Just make sure sine, cosine, angle_reduce, and the float comparison functions behave as described. If you end up using a different tool, the input to the bashcalc() function does not nee to match the input format mentioned below as it is determined by your tool choice.

Required Files

The file ~cs9e-1/hw/04.01-bashcalc/bashcalc-fw.sh contains the framework for this assignment. Save this file as bashcalc-functions.sh. You will be implementing the functions in this file for parts 2 and 3 of this assignment. You are free to use it as a starting point or start from scratch if you desire.

Assignment

Part 1 - Simple BASH Scripting

Create a script called 'bashcalc-1.sh' which takes an expression on the command line, causes 'bc -l' to evaluate it, and prints the results.

Example - Compute the sine of pi/4:

$ ./bashcalc-1.sh "s(3.14159*0.25)"
.70710631209355760535

Part 2 - BASH Functions

If we enter a situation where we need to make many calls to bashcalc, keeping it as a seperate script is not very practical. This is because the operating system needs to create a new process for each script being executed. Therefore, we should convert this script to a BASH function.

Complete the implementation of the bashcalc() function in bashcalc-functions.sh (see 'required files' above). After sourcing this file, you should have a bashcalc() function in your environment with the exact same functionality as your bashcalc-1.sh script.

Example - Compute the sine of pi/4:

$ . bashcalc-functions.sh
$ bashcalc "s(3.14159*0.25)"
.70710631209355760535

Part 3 - Additional Functions

The following lines were placed in bashcalc-functions.sh to avoid parse errors due to empty function bodies while you were working on part 2. Please remove them before you implement part 3:

# Remove this line when you start part 3
return 0
Complete the implementation of the following functions:
  1. sine
  2. cosine
  3. angle_reduce (Hint: The expression '8*a(1)' in bc will return 2*pi)
  4. float_lt, float_lte, float_eq (Hint: Try common comparison operators in bc)

After implementing these functions, you shoule be able to call them after resourcing bashcalc-functions.sh.

Example:

$ . bashcalc-functions.sh
$ sine "3.14159*0.25"
.70710631209355760535

Bring in transcripts showing your tests for all these functions. Make sure you test sufficient cases to be satisfied with the function's correctness.

Note

The finished version of bash-functions.sh will be used in your next assignment, so don't loose it.

Assignment Checklist

Part 1

  • Student has correctly working code.
  • Student shows sufficient test cases.
  • First line correctly causes bash to be used as the interpreter.

Part 2

  • Student has correctly working code.
  • Student shows sufficient test cases to catch possible corner cases.
  • bashcalc-functions.sh has no code outside of function blocks.
  • bashcalc() behaves identically to the bashcalc-1.sh script from part 1.

Part 3

  • Student has correctly working code.
  • Student shows sufficient test cases to catch possible corner cases.
  • Student uses proper abstraction and does not call 'bc' outside of the bashcalc() function.

All

  • Student correctly uses quotes where appropriate
  • Student correctly uses integer, text, and floating point comparisons