1277 lines
24 KiB
HTML
1277 lines
24 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
|
<HTML
|
|
><HEAD
|
|
><TITLE
|
|
>Math Commands</TITLE
|
|
><META
|
|
NAME="GENERATOR"
|
|
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
|
|
REL="HOME"
|
|
TITLE="Advanced Bash-Scripting Guide"
|
|
HREF="index.html"><LINK
|
|
REL="UP"
|
|
TITLE="External Filters, Programs and Commands"
|
|
HREF="external.html"><LINK
|
|
REL="PREVIOUS"
|
|
TITLE="Terminal Control Commands"
|
|
HREF="terminalccmds.html"><LINK
|
|
REL="NEXT"
|
|
TITLE="Miscellaneous Commands"
|
|
HREF="extmisc.html"></HEAD
|
|
><BODY
|
|
CLASS="SECT1"
|
|
BGCOLOR="#FFFFFF"
|
|
TEXT="#000000"
|
|
LINK="#0000FF"
|
|
VLINK="#840084"
|
|
ALINK="#0000FF"
|
|
><DIV
|
|
CLASS="NAVHEADER"
|
|
><TABLE
|
|
SUMMARY="Header navigation table"
|
|
WIDTH="100%"
|
|
BORDER="0"
|
|
CELLPADDING="0"
|
|
CELLSPACING="0"
|
|
><TR
|
|
><TH
|
|
COLSPAN="3"
|
|
ALIGN="center"
|
|
>Advanced Bash-Scripting Guide: </TH
|
|
></TR
|
|
><TR
|
|
><TD
|
|
WIDTH="10%"
|
|
ALIGN="left"
|
|
VALIGN="bottom"
|
|
><A
|
|
HREF="terminalccmds.html"
|
|
ACCESSKEY="P"
|
|
>Prev</A
|
|
></TD
|
|
><TD
|
|
WIDTH="80%"
|
|
ALIGN="center"
|
|
VALIGN="bottom"
|
|
>Chapter 16. External Filters, Programs and Commands</TD
|
|
><TD
|
|
WIDTH="10%"
|
|
ALIGN="right"
|
|
VALIGN="bottom"
|
|
><A
|
|
HREF="extmisc.html"
|
|
ACCESSKEY="N"
|
|
>Next</A
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
><HR
|
|
ALIGN="LEFT"
|
|
WIDTH="100%"></DIV
|
|
><DIV
|
|
CLASS="SECT1"
|
|
><H1
|
|
CLASS="SECT1"
|
|
><A
|
|
NAME="MATHC"
|
|
></A
|
|
>16.8. Math Commands</H1
|
|
><P
|
|
></P
|
|
><DIV
|
|
CLASS="VARIABLELIST"
|
|
><P
|
|
><B
|
|
><A
|
|
NAME="MATHCOMMANDLISTING1"
|
|
></A
|
|
><SPAN
|
|
CLASS="QUOTE"
|
|
>"Doing the
|
|
numbers"</SPAN
|
|
></B
|
|
></P
|
|
><DL
|
|
><DT
|
|
><A
|
|
NAME="FACTORREF"
|
|
></A
|
|
><B
|
|
CLASS="COMMAND"
|
|
>factor</B
|
|
></DT
|
|
><DD
|
|
><P
|
|
>Decompose an integer into prime factors.</P
|
|
><P
|
|
> <TABLE
|
|
BORDER="1"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="90%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
><TT
|
|
CLASS="PROMPT"
|
|
>bash$ </TT
|
|
><TT
|
|
CLASS="USERINPUT"
|
|
><B
|
|
>factor 27417</B
|
|
></TT
|
|
>
|
|
<TT
|
|
CLASS="COMPUTEROUTPUT"
|
|
>27417: 3 13 19 37</TT
|
|
>
|
|
</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
>
|
|
</P
|
|
><DIV
|
|
CLASS="EXAMPLE"
|
|
><A
|
|
NAME="PRIMES2"
|
|
></A
|
|
><P
|
|
><B
|
|
>Example 16-46. Generating prime numbers</B
|
|
></P
|
|
><TABLE
|
|
BORDER="0"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="90%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="PROGRAMLISTING"
|
|
>#!/bin/bash
|
|
# primes2.sh
|
|
|
|
# Generating prime numbers the quick-and-easy way,
|
|
#+ without resorting to fancy algorithms.
|
|
|
|
CEILING=10000 # 1 to 10000
|
|
PRIME=0
|
|
E_NOTPRIME=
|
|
|
|
is_prime ()
|
|
{
|
|
local factors
|
|
factors=( $(factor $1) ) # Load output of `factor` into array.
|
|
|
|
if [ -z "${factors[2]}" ]
|
|
# Third element of "factors" array:
|
|
#+ ${factors[2]} is 2nd factor of argument.
|
|
# If it is blank, then there is no 2nd factor,
|
|
#+ and the argument is therefore prime.
|
|
then
|
|
return $PRIME # 0
|
|
else
|
|
return $E_NOTPRIME # null
|
|
fi
|
|
}
|
|
|
|
echo
|
|
for n in $(seq $CEILING)
|
|
do
|
|
if is_prime $n
|
|
then
|
|
printf %5d $n
|
|
fi # ^ Five positions per number suffices.
|
|
done # For a higher $CEILING, adjust upward, as necessary.
|
|
|
|
echo
|
|
|
|
exit</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
></DIV
|
|
></DD
|
|
><DT
|
|
><A
|
|
NAME="BCREF"
|
|
></A
|
|
><B
|
|
CLASS="COMMAND"
|
|
>bc</B
|
|
></DT
|
|
><DD
|
|
><P
|
|
>Bash can't handle floating point calculations, and
|
|
it lacks operators for certain important mathematical
|
|
functions. Fortunately, <B
|
|
CLASS="COMMAND"
|
|
>bc</B
|
|
> gallops to
|
|
the rescue.</P
|
|
><P
|
|
>Not just a versatile, arbitrary precision calculation
|
|
utility, <B
|
|
CLASS="COMMAND"
|
|
>bc</B
|
|
> offers many of the facilities of
|
|
a programming language. It has a syntax vaguely resembling
|
|
<B
|
|
CLASS="COMMAND"
|
|
>C</B
|
|
>.</P
|
|
><P
|
|
>Since it is a fairly well-behaved UNIX utility, and may
|
|
therefore be used in a <A
|
|
HREF="special-chars.html#PIPEREF"
|
|
>pipe</A
|
|
>,
|
|
<B
|
|
CLASS="COMMAND"
|
|
>bc</B
|
|
> comes in handy in scripts.</P
|
|
><P
|
|
><A
|
|
NAME="BCTEMPLATE"
|
|
></A
|
|
></P
|
|
><P
|
|
>Here is a simple template for using
|
|
<B
|
|
CLASS="COMMAND"
|
|
>bc</B
|
|
> to calculate a script
|
|
variable. This uses <A
|
|
HREF="commandsub.html#COMMANDSUBREF"
|
|
>command
|
|
substitution</A
|
|
>.</P
|
|
><P
|
|
> <TABLE
|
|
BORDER="1"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="90%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
> <TT
|
|
CLASS="USERINPUT"
|
|
><B
|
|
>variable=$(echo "OPTIONS; OPERATIONS" | bc)</B
|
|
></TT
|
|
>
|
|
</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
>
|
|
</P
|
|
><P
|
|
><A
|
|
NAME="MONTHLYPMT0"
|
|
></A
|
|
></P
|
|
><DIV
|
|
CLASS="EXAMPLE"
|
|
><A
|
|
NAME="MONTHLYPMT"
|
|
></A
|
|
><P
|
|
><B
|
|
>Example 16-47. Monthly Payment on a Mortgage</B
|
|
></P
|
|
><TABLE
|
|
BORDER="0"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="90%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="PROGRAMLISTING"
|
|
>#!/bin/bash
|
|
# monthlypmt.sh: Calculates monthly payment on a mortgage.
|
|
|
|
|
|
# This is a modification of code in the
|
|
#+ "mcalc" (mortgage calculator) package,
|
|
#+ by Jeff Schmidt
|
|
#+ and
|
|
#+ Mendel Cooper (yours truly, the ABS Guide author).
|
|
# http://www.ibiblio.org/pub/Linux/apps/financial/mcalc-1.6.tar.gz
|
|
|
|
echo
|
|
echo "Given the principal, interest rate, and term of a mortgage,"
|
|
echo "calculate the monthly payment."
|
|
|
|
bottom=1.0
|
|
|
|
echo
|
|
echo -n "Enter principal (no commas) "
|
|
read principal
|
|
echo -n "Enter interest rate (percent) " # If 12%, enter "12", not ".12".
|
|
read interest_r
|
|
echo -n "Enter term (months) "
|
|
read term
|
|
|
|
|
|
interest_r=$(echo "scale=9; $interest_r/100.0" | bc) # Convert to decimal.
|
|
# ^^^^^^^^^^^^^^^^^ Divide by 100.
|
|
# "scale" determines how many decimal places.
|
|
|
|
interest_rate=$(echo "scale=9; $interest_r/12 + 1.0" | bc)
|
|
|
|
|
|
top=$(echo "scale=9; $principal*$interest_rate^$term" | bc)
|
|
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
# Standard formula for figuring interest.
|
|
|
|
echo; echo "Please be patient. This may take a while."
|
|
|
|
let "months = $term - 1"
|
|
# ====================================================================
|
|
for ((x=$months; x > 0; x--))
|
|
do
|
|
bot=$(echo "scale=9; $interest_rate^$x" | bc)
|
|
bottom=$(echo "scale=9; $bottom+$bot" | bc)
|
|
# bottom = $(($bottom + $bot"))
|
|
done
|
|
# ====================================================================
|
|
|
|
# --------------------------------------------------------------------
|
|
# Rick Boivie pointed out a more efficient implementation
|
|
#+ of the above loop, which decreases computation time by 2/3.
|
|
|
|
# for ((x=1; x <= $months; x++))
|
|
# do
|
|
# bottom=$(echo "scale=9; $bottom * $interest_rate + 1" | bc)
|
|
# done
|
|
|
|
|
|
# And then he came up with an even more efficient alternative,
|
|
#+ one that cuts down the run time by about 95%!
|
|
|
|
# bottom=`{
|
|
# echo "scale=9; bottom=$bottom; interest_rate=$interest_rate"
|
|
# for ((x=1; x <= $months; x++))
|
|
# do
|
|
# echo 'bottom = bottom * interest_rate + 1'
|
|
# done
|
|
# echo 'bottom'
|
|
# } | bc` # Embeds a 'for loop' within command substitution.
|
|
# --------------------------------------------------------------------------
|
|
# On the other hand, Frank Wang suggests:
|
|
# bottom=$(echo "scale=9; ($interest_rate^$term-1)/($interest_rate-1)" | bc)
|
|
|
|
# Because . . .
|
|
# The algorithm behind the loop
|
|
#+ is actually a sum of geometric proportion series.
|
|
# The sum formula is e0(1-q^n)/(1-q),
|
|
#+ where e0 is the first element and q=e(n+1)/e(n)
|
|
#+ and n is the number of elements.
|
|
# --------------------------------------------------------------------------
|
|
|
|
|
|
# let "payment = $top/$bottom"
|
|
payment=$(echo "scale=2; $top/$bottom" | bc)
|
|
# Use two decimal places for dollars and cents.
|
|
|
|
echo
|
|
echo "monthly payment = \$$payment" # Echo a dollar sign in front of amount.
|
|
echo
|
|
|
|
|
|
exit 0
|
|
|
|
|
|
# Exercises:
|
|
# 1) Filter input to permit commas in principal amount.
|
|
# 2) Filter input to permit interest to be entered as percent or decimal.
|
|
# 3) If you are really ambitious,
|
|
#+ expand this script to print complete amortization tables.</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
></DIV
|
|
><P
|
|
><A
|
|
NAME="BASE0"
|
|
></A
|
|
></P
|
|
><DIV
|
|
CLASS="EXAMPLE"
|
|
><A
|
|
NAME="BASE"
|
|
></A
|
|
><P
|
|
><B
|
|
>Example 16-48. Base Conversion</B
|
|
></P
|
|
><TABLE
|
|
BORDER="0"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="90%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="PROGRAMLISTING"
|
|
>#!/bin/bash
|
|
###########################################################################
|
|
# Shellscript: base.sh - print number to different bases (Bourne Shell)
|
|
# Author : Heiner Steven (heiner.steven@odn.de)
|
|
# Date : 07-03-95
|
|
# Category : Desktop
|
|
# $Id: base.sh,v 1.2 2000/02/06 19:55:35 heiner Exp $
|
|
# ==> Above line is RCS ID info.
|
|
###########################################################################
|
|
# Description
|
|
#
|
|
# Changes
|
|
# 21-03-95 stv fixed error occuring with 0xb as input (0.2)
|
|
###########################################################################
|
|
|
|
# ==> Used in ABS Guide with the script author's permission.
|
|
# ==> Comments added by ABS Guide author.
|
|
|
|
NOARGS=85
|
|
PN=`basename "$0"` # Program name
|
|
VER=`echo '$Revision: 1.2 $' | cut -d' ' -f2` # ==> VER=1.2
|
|
|
|
Usage () {
|
|
echo "$PN - print number to different bases, $VER (stv '95)
|
|
usage: $PN [number ...]
|
|
|
|
If no number is given, the numbers are read from standard input.
|
|
A number may be
|
|
binary (base 2) starting with 0b (i.e. 0b1100)
|
|
octal (base 8) starting with 0 (i.e. 014)
|
|
hexadecimal (base 16) starting with 0x (i.e. 0xc)
|
|
decimal otherwise (i.e. 12)" >&2
|
|
exit $NOARGS
|
|
} # ==> Prints usage message.
|
|
|
|
Msg () {
|
|
for i # ==> in [list] missing. Why?
|
|
do echo "$PN: $i" >&2
|
|
done
|
|
}
|
|
|
|
Fatal () { Msg "$@"; exit 66; }
|
|
|
|
PrintBases () {
|
|
# Determine base of the number
|
|
for i # ==> in [list] missing...
|
|
do # ==> so operates on command-line arg(s).
|
|
case "$i" in
|
|
0b*) ibase=2;; # binary
|
|
0x*|[a-f]*|[A-F]*) ibase=16;; # hexadecimal
|
|
0*) ibase=8;; # octal
|
|
[1-9]*) ibase=10;; # decimal
|
|
*)
|
|
Msg "illegal number $i - ignored"
|
|
continue;;
|
|
esac
|
|
|
|
# Remove prefix, convert hex digits to uppercase (bc needs this).
|
|
number=`echo "$i" | sed -e 's:^0[bBxX]::' | tr '[a-f]' '[A-F]'`
|
|
# ==> Uses ":" as sed separator, rather than "/".
|
|
|
|
# Convert number to decimal
|
|
dec=`echo "ibase=$ibase; $number" | bc` # ==> 'bc' is calculator utility.
|
|
case "$dec" in
|
|
[0-9]*) ;; # number ok
|
|
*) continue;; # error: ignore
|
|
esac
|
|
|
|
# Print all conversions in one line.
|
|
# ==> 'here document' feeds command list to 'bc'.
|
|
echo `bc <<!
|
|
obase=16; "hex="; $dec
|
|
obase=10; "dec="; $dec
|
|
obase=8; "oct="; $dec
|
|
obase=2; "bin="; $dec
|
|
!
|
|
` | sed -e 's: : :g'
|
|
|
|
done
|
|
}
|
|
|
|
while [ $# -gt 0 ]
|
|
# ==> Is a "while loop" really necessary here,
|
|
# ==>+ since all the cases either break out of the loop
|
|
# ==>+ or terminate the script.
|
|
# ==> (Above comment by Paulo Marcel Coelho Aragao.)
|
|
do
|
|
case "$1" in
|
|
--) shift; break;;
|
|
-h) Usage;; # ==> Help message.
|
|
-*) Usage;;
|
|
*) break;; # First number
|
|
esac # ==> Error checking for illegal input might be appropriate.
|
|
shift
|
|
done
|
|
|
|
if [ $# -gt 0 ]
|
|
then
|
|
PrintBases "$@"
|
|
else # Read from stdin.
|
|
while read line
|
|
do
|
|
PrintBases $line
|
|
done
|
|
fi
|
|
|
|
|
|
exit</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
></DIV
|
|
><P
|
|
><A
|
|
NAME="BCHEREDOC"
|
|
></A
|
|
></P
|
|
><P
|
|
>An alternate method of invoking <B
|
|
CLASS="COMMAND"
|
|
>bc</B
|
|
>
|
|
involves using a <A
|
|
HREF="here-docs.html#HEREDOCREF"
|
|
>here
|
|
document</A
|
|
> embedded within a <A
|
|
HREF="commandsub.html#COMMANDSUBREF"
|
|
>command substitution</A
|
|
>
|
|
block. This is especially appropriate when a script
|
|
needs to pass a list of options and commands to
|
|
<B
|
|
CLASS="COMMAND"
|
|
>bc</B
|
|
>.</P
|
|
><P
|
|
> <TABLE
|
|
BORDER="0"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="90%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="PROGRAMLISTING"
|
|
>variable=`bc << LIMIT_STRING
|
|
options
|
|
statements
|
|
operations
|
|
LIMIT_STRING
|
|
`
|
|
|
|
...or...
|
|
|
|
|
|
variable=$(bc << LIMIT_STRING
|
|
options
|
|
statements
|
|
operations
|
|
LIMIT_STRING
|
|
)</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
>
|
|
</P
|
|
><DIV
|
|
CLASS="EXAMPLE"
|
|
><A
|
|
NAME="ALTBC"
|
|
></A
|
|
><P
|
|
><B
|
|
>Example 16-49. Invoking <I
|
|
CLASS="FIRSTTERM"
|
|
>bc</I
|
|
> using a <I
|
|
CLASS="FIRSTTERM"
|
|
>here
|
|
document</I
|
|
></B
|
|
></P
|
|
><TABLE
|
|
BORDER="0"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="90%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="PROGRAMLISTING"
|
|
>#!/bin/bash
|
|
# Invoking 'bc' using command substitution
|
|
# in combination with a 'here document'.
|
|
|
|
|
|
var1=`bc << EOF
|
|
18.33 * 19.78
|
|
EOF
|
|
`
|
|
echo $var1 # 362.56
|
|
|
|
|
|
# $( ... ) notation also works.
|
|
v1=23.53
|
|
v2=17.881
|
|
v3=83.501
|
|
v4=171.63
|
|
|
|
var2=$(bc << EOF
|
|
scale = 4
|
|
a = ( $v1 + $v2 )
|
|
b = ( $v3 * $v4 )
|
|
a * b + 15.35
|
|
EOF
|
|
)
|
|
echo $var2 # 593487.8452
|
|
|
|
|
|
var3=$(bc -l << EOF
|
|
scale = 9
|
|
s ( 1.7 )
|
|
EOF
|
|
)
|
|
# Returns the sine of 1.7 radians.
|
|
# The "-l" option calls the 'bc' math library.
|
|
echo $var3 # .991664810
|
|
|
|
|
|
# Now, try it in a function...
|
|
hypotenuse () # Calculate hypotenuse of a right triangle.
|
|
{ # c = sqrt( a^2 + b^2 )
|
|
hyp=$(bc -l << EOF
|
|
scale = 9
|
|
sqrt ( $1 * $1 + $2 * $2 )
|
|
EOF
|
|
)
|
|
# Can't directly return floating point values from a Bash function.
|
|
# But, can echo-and-capture:
|
|
echo "$hyp"
|
|
}
|
|
|
|
hyp=$(hypotenuse 3.68 7.31)
|
|
echo "hypotenuse = $hyp" # 8.184039344
|
|
|
|
|
|
exit 0</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
></DIV
|
|
><P
|
|
><A
|
|
NAME="CANNONREF"
|
|
></A
|
|
></P
|
|
><DIV
|
|
CLASS="EXAMPLE"
|
|
><A
|
|
NAME="CANNON"
|
|
></A
|
|
><P
|
|
><B
|
|
>Example 16-50. Calculating PI</B
|
|
></P
|
|
><TABLE
|
|
BORDER="0"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="90%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="PROGRAMLISTING"
|
|
>#!/bin/bash
|
|
# cannon.sh: Approximating PI by firing cannonballs.
|
|
|
|
# Author: Mendel Cooper
|
|
# License: Public Domain
|
|
# Version 2.2, reldate 13oct08.
|
|
|
|
# This is a very simple instance of a "Monte Carlo" simulation:
|
|
#+ a mathematical model of a real-life event,
|
|
#+ using pseudorandom numbers to emulate random chance.
|
|
|
|
# Consider a perfectly square plot of land, 10000 units on a side.
|
|
# This land has a perfectly circular lake in its center,
|
|
#+ with a diameter of 10000 units.
|
|
# The plot is actually mostly water, except for land in the four corners.
|
|
# (Think of it as a square with an inscribed circle.)
|
|
#
|
|
# We will fire iron cannonballs from an old-style cannon
|
|
#+ at the square.
|
|
# All the shots impact somewhere on the square,
|
|
#+ either in the lake or on the dry corners.
|
|
# Since the lake takes up most of the area,
|
|
#+ most of the shots will SPLASH! into the water.
|
|
# Just a few shots will THUD! into solid ground
|
|
#+ in the four corners of the square.
|
|
#
|
|
# If we take enough random, unaimed shots at the square,
|
|
#+ Then the ratio of SPLASHES to total shots will approximate
|
|
#+ the value of PI/4.
|
|
#
|
|
# The simplified explanation is that the cannon is actually
|
|
#+ shooting only at the upper right-hand quadrant of the square,
|
|
#+ i.e., Quadrant I of the Cartesian coordinate plane.
|
|
#
|
|
#
|
|
# Theoretically, the more shots taken, the better the fit.
|
|
# However, a shell script, as opposed to a compiled language
|
|
#+ with floating-point math built in, requires some compromises.
|
|
# This decreases the accuracy of the simulation.
|
|
|
|
|
|
DIMENSION=10000 # Length of each side of the plot.
|
|
# Also sets ceiling for random integers generated.
|
|
|
|
MAXSHOTS=1000 # Fire this many shots.
|
|
# 10000 or more would be better, but would take too long.
|
|
PMULTIPLIER=4.0 # Scaling factor.
|
|
|
|
declare -r M_PI=3.141592654
|
|
# Actual 9-place value of PI, for comparison purposes.
|
|
|
|
get_random ()
|
|
{
|
|
SEED=$(head -n 1 /dev/urandom | od -N 1 | awk '{ print $2 }')
|
|
RANDOM=$SEED # From "seeding-random.sh"
|
|
#+ example script.
|
|
let "rnum = $RANDOM % $DIMENSION" # Range less than 10000.
|
|
echo $rnum
|
|
}
|
|
|
|
distance= # Declare global variable.
|
|
hypotenuse () # Calculate hypotenuse of a right triangle.
|
|
{ # From "alt-bc.sh" example.
|
|
distance=$(bc -l << EOF
|
|
scale = 0
|
|
sqrt ( $1 * $1 + $2 * $2 )
|
|
EOF
|
|
)
|
|
# Setting "scale" to zero rounds down result to integer value,
|
|
#+ a necessary compromise in this script.
|
|
# It decreases the accuracy of this simulation.
|
|
}
|
|
|
|
|
|
# ==========================================================
|
|
# main() {
|
|
# "Main" code block, mimicking a C-language main() function.
|
|
|
|
# Initialize variables.
|
|
shots=0
|
|
splashes=0
|
|
thuds=0
|
|
Pi=0
|
|
error=0
|
|
|
|
while [ "$shots" -lt "$MAXSHOTS" ] # Main loop.
|
|
do
|
|
|
|
xCoord=$(get_random) # Get random X and Y coords.
|
|
yCoord=$(get_random)
|
|
hypotenuse $xCoord $yCoord # Hypotenuse of
|
|
#+ right-triangle = distance.
|
|
((shots++))
|
|
|
|
printf "#%4d " $shots
|
|
printf "Xc = %4d " $xCoord
|
|
printf "Yc = %4d " $yCoord
|
|
printf "Distance = %5d " $distance # Distance from
|
|
#+ center of lake
|
|
#+ -- the "origin" --
|
|
#+ coordinate (0,0).
|
|
|
|
if [ "$distance" -le "$DIMENSION" ]
|
|
then
|
|
echo -n "SPLASH! "
|
|
((splashes++))
|
|
else
|
|
echo -n "THUD! "
|
|
((thuds++))
|
|
fi
|
|
|
|
Pi=$(echo "scale=9; $PMULTIPLIER*$splashes/$shots" | bc)
|
|
# Multiply ratio by 4.0.
|
|
echo -n "PI ~ $Pi"
|
|
echo
|
|
|
|
done
|
|
|
|
echo
|
|
echo "After $shots shots, PI looks like approximately $Pi"
|
|
# Tends to run a bit high,
|
|
#+ possibly due to round-off error and imperfect randomness of $RANDOM.
|
|
# But still usually within plus-or-minus 5% . . .
|
|
#+ a pretty fair rough approximation.
|
|
error=$(echo "scale=9; $Pi - $M_PI" | bc)
|
|
pct_error=$(echo "scale=2; 100.0 * $error / $M_PI" | bc)
|
|
echo -n "Deviation from mathematical value of PI = $error"
|
|
echo " ($pct_error% error)"
|
|
echo
|
|
|
|
# End of "main" code block.
|
|
# }
|
|
# ==========================================================
|
|
|
|
exit 0
|
|
|
|
# One might well wonder whether a shell script is appropriate for
|
|
#+ an application as complex and computation-intensive as a simulation.
|
|
#
|
|
# There are at least two justifications.
|
|
# 1) As a proof of concept: to show it can be done.
|
|
# 2) To prototype and test the algorithms before rewriting
|
|
#+ it in a compiled high-level language.</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
></DIV
|
|
><P
|
|
>See also <A
|
|
HREF="contributed-scripts.html#STDDEV"
|
|
>Example A-37</A
|
|
>.</P
|
|
></DD
|
|
><DT
|
|
><A
|
|
NAME="DCREF"
|
|
></A
|
|
><B
|
|
CLASS="COMMAND"
|
|
>dc</B
|
|
></DT
|
|
><DD
|
|
><P
|
|
>The <B
|
|
CLASS="COMMAND"
|
|
>dc</B
|
|
> (<B
|
|
CLASS="COMMAND"
|
|
>d</B
|
|
>esk
|
|
<B
|
|
CLASS="COMMAND"
|
|
>c</B
|
|
>alculator) utility is <A
|
|
HREF="internalvariables.html#STACKDEFREF"
|
|
>stack-oriented</A
|
|
>
|
|
and uses RPN (<I
|
|
CLASS="FIRSTTERM"
|
|
>Reverse Polish Notation</I
|
|
>).
|
|
Like <B
|
|
CLASS="COMMAND"
|
|
>bc</B
|
|
>, it has much of the power of
|
|
a programming language.</P
|
|
><P
|
|
>Similar to the procedure with <B
|
|
CLASS="COMMAND"
|
|
>bc</B
|
|
>,
|
|
<A
|
|
HREF="internal.html#ECHOREF"
|
|
>echo</A
|
|
> a command-string
|
|
to <B
|
|
CLASS="COMMAND"
|
|
>dc</B
|
|
>.</P
|
|
><P
|
|
> <TABLE
|
|
BORDER="0"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="90%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="PROGRAMLISTING"
|
|
>echo "[Printing a string ... ]P" | dc
|
|
# The P command prints the string between the preceding brackets.
|
|
|
|
# And now for some simple arithmetic.
|
|
echo "7 8 * p" | dc # 56
|
|
# Pushes 7, then 8 onto the stack,
|
|
#+ multiplies ("*" operator), then prints the result ("p" operator).</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
>
|
|
</P
|
|
><P
|
|
>Most persons avoid <B
|
|
CLASS="COMMAND"
|
|
>dc</B
|
|
>, because
|
|
of its non-intuitive input and rather cryptic
|
|
operators. Yet, it has its uses.</P
|
|
><DIV
|
|
CLASS="EXAMPLE"
|
|
><A
|
|
NAME="HEXCONVERT"
|
|
></A
|
|
><P
|
|
><B
|
|
>Example 16-51. Converting a decimal number to hexadecimal</B
|
|
></P
|
|
><TABLE
|
|
BORDER="0"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="90%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="PROGRAMLISTING"
|
|
>#!/bin/bash
|
|
# hexconvert.sh: Convert a decimal number to hexadecimal.
|
|
|
|
E_NOARGS=85 # Command-line arg missing.
|
|
BASE=16 # Hexadecimal.
|
|
|
|
if [ -z "$1" ]
|
|
then # Need a command-line argument.
|
|
echo "Usage: $0 number"
|
|
exit $E_NOARGS
|
|
fi # Exercise: add argument validity checking.
|
|
|
|
|
|
hexcvt ()
|
|
{
|
|
if [ -z "$1" ]
|
|
then
|
|
echo 0
|
|
return # "Return" 0 if no arg passed to function.
|
|
fi
|
|
|
|
echo ""$1" "$BASE" o p" | dc
|
|
# o sets radix (numerical base) of output.
|
|
# p prints the top of stack.
|
|
# For other options: 'man dc' ...
|
|
return
|
|
}
|
|
|
|
hexcvt "$1"
|
|
|
|
exit</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
></DIV
|
|
><P
|
|
>Studying the <A
|
|
HREF="basic.html#INFOREF"
|
|
>info</A
|
|
> page for
|
|
<B
|
|
CLASS="COMMAND"
|
|
>dc</B
|
|
> is a painful path to understanding its
|
|
intricacies. There seems to be a small, select group of
|
|
<EM
|
|
>dc wizards</EM
|
|
> who delight in showing off
|
|
their mastery of this powerful, but arcane utility.</P
|
|
><P
|
|
> <TABLE
|
|
BORDER="1"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="90%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
><TT
|
|
CLASS="PROMPT"
|
|
>bash$ </TT
|
|
><TT
|
|
CLASS="USERINPUT"
|
|
><B
|
|
>echo "16i[q]sa[ln0=aln100%Pln100/snlbx]sbA0D68736142snlbxq" | dc</B
|
|
></TT
|
|
>
|
|
<TT
|
|
CLASS="COMPUTEROUTPUT"
|
|
>Bash</TT
|
|
>
|
|
</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
>
|
|
</P
|
|
><P
|
|
><A
|
|
NAME="GOLDENRATIO"
|
|
></A
|
|
>
|
|
<TABLE
|
|
BORDER="0"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="90%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="PROGRAMLISTING"
|
|
>dc <<< 10k5v1+2/p # 1.6180339887
|
|
# ^^^ Feed operations to dc using a Here String.
|
|
# ^^^ Pushes 10 and sets that as the precision (10k).
|
|
# ^^ Pushes 5 and takes its square root
|
|
# (5v, v = square root).
|
|
# ^^ Pushes 1 and adds it to the running total (1+).
|
|
# ^^ Pushes 2 and divides the running total by that (2/).
|
|
# ^ Pops and prints the result (p)
|
|
# The result is 1.6180339887 ...
|
|
# ... which happens to be the Pythagorean Golden Ratio, to 10 places.</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
>
|
|
</P
|
|
><DIV
|
|
CLASS="EXAMPLE"
|
|
><A
|
|
NAME="FACTR"
|
|
></A
|
|
><P
|
|
><B
|
|
>Example 16-52. Factoring</B
|
|
></P
|
|
><TABLE
|
|
BORDER="0"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="90%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="PROGRAMLISTING"
|
|
>#!/bin/bash
|
|
# factr.sh: Factor a number
|
|
|
|
MIN=2 # Will not work for number smaller than this.
|
|
E_NOARGS=85
|
|
E_TOOSMALL=86
|
|
|
|
if [ -z $1 ]
|
|
then
|
|
echo "Usage: $0 number"
|
|
exit $E_NOARGS
|
|
fi
|
|
|
|
if [ "$1" -lt "$MIN" ]
|
|
then
|
|
echo "Number to factor must be $MIN or greater."
|
|
exit $E_TOOSMALL
|
|
fi
|
|
|
|
# Exercise: Add type checking (to reject non-integer arg).
|
|
|
|
echo "Factors of $1:"
|
|
# -------------------------------------------------------
|
|
echo "$1[p]s2[lip/dli%0=1dvsr]s12sid2%0=13sidvsr[dli%0=\
|
|
1lrli2+dsi!>.]ds.xd1<2" | dc
|
|
# -------------------------------------------------------
|
|
# Above code written by Michel Charpentier <charpov@cs.unh.edu>
|
|
# (as a one-liner, here broken into two lines for display purposes).
|
|
# Used in ABS Guide with permission (thanks!).
|
|
|
|
exit
|
|
|
|
# $ sh factr.sh 270138
|
|
# 2
|
|
# 3
|
|
# 11
|
|
# 4093</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
></DIV
|
|
></DD
|
|
><DT
|
|
><A
|
|
NAME="AWKMATH"
|
|
></A
|
|
><B
|
|
CLASS="COMMAND"
|
|
>awk</B
|
|
></DT
|
|
><DD
|
|
><P
|
|
>Yet another way of doing floating point math in
|
|
a script is using <A
|
|
HREF="awk.html#AWKREF"
|
|
>awk's</A
|
|
>
|
|
built-in math functions in a <A
|
|
HREF="wrapper.html#SHWRAPPER"
|
|
>shell
|
|
wrapper</A
|
|
>.</P
|
|
><DIV
|
|
CLASS="EXAMPLE"
|
|
><A
|
|
NAME="HYPOT"
|
|
></A
|
|
><P
|
|
><B
|
|
>Example 16-53. Calculating the hypotenuse of a triangle</B
|
|
></P
|
|
><TABLE
|
|
BORDER="0"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="90%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="PROGRAMLISTING"
|
|
>#!/bin/bash
|
|
# hypotenuse.sh: Returns the "hypotenuse" of a right triangle.
|
|
# (square root of sum of squares of the "legs")
|
|
|
|
ARGS=2 # Script needs sides of triangle passed.
|
|
E_BADARGS=85 # Wrong number of arguments.
|
|
|
|
if [ $# -ne "$ARGS" ] # Test number of arguments to script.
|
|
then
|
|
echo "Usage: `basename $0` side_1 side_2"
|
|
exit $E_BADARGS
|
|
fi
|
|
|
|
|
|
AWKSCRIPT=' { printf( "%3.7f\n", sqrt($1*$1 + $2*$2) ) } '
|
|
# command(s) / parameters passed to awk
|
|
|
|
|
|
# Now, pipe the parameters to awk.
|
|
echo -n "Hypotenuse of $1 and $2 = "
|
|
echo $1 $2 | awk "$AWKSCRIPT"
|
|
# ^^^^^^^^^^^^
|
|
# An echo-and-pipe is an easy way of passing shell parameters to awk.
|
|
|
|
exit
|
|
|
|
# Exercise: Rewrite this script using 'bc' rather than awk.
|
|
# Which method is more intuitive?</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
></DIV
|
|
></DD
|
|
></DL
|
|
></DIV
|
|
></DIV
|
|
><DIV
|
|
CLASS="NAVFOOTER"
|
|
><HR
|
|
ALIGN="LEFT"
|
|
WIDTH="100%"><TABLE
|
|
SUMMARY="Footer navigation table"
|
|
WIDTH="100%"
|
|
BORDER="0"
|
|
CELLPADDING="0"
|
|
CELLSPACING="0"
|
|
><TR
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="left"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="terminalccmds.html"
|
|
ACCESSKEY="P"
|
|
>Prev</A
|
|
></TD
|
|
><TD
|
|
WIDTH="34%"
|
|
ALIGN="center"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="index.html"
|
|
ACCESSKEY="H"
|
|
>Home</A
|
|
></TD
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="right"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="extmisc.html"
|
|
ACCESSKEY="N"
|
|
>Next</A
|
|
></TD
|
|
></TR
|
|
><TR
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="left"
|
|
VALIGN="top"
|
|
>Terminal Control Commands</TD
|
|
><TD
|
|
WIDTH="34%"
|
|
ALIGN="center"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="external.html"
|
|
ACCESSKEY="U"
|
|
>Up</A
|
|
></TD
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="right"
|
|
VALIGN="top"
|
|
>Miscellaneous Commands</TD
|
|
></TR
|
|
></TABLE
|
|
></DIV
|
|
></BODY
|
|
></HTML
|
|
> |