1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Math Commands</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
"><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"><META
HTTP-EQUIV="Content-Style-Type"
CONTENT="text/css"><LINK
REL="stylesheet"
HREF="common/kde-common.css"
TYPE="text/css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=iso-8859-1"><META
HTTP-EQUIV="Content-Language"
CONTENT="en"><LINK
REL="stylesheet"
HREF="common/kde-localised.css"
TYPE="text/css"
TITLE="KDE-English"><LINK
REL="stylesheet"
HREF="common/kde-default.css"
TYPE="text/css"
TITLE="KDE-Default"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#AA0000"
VLINK="#AA0055"
ALINK="#AA0000"
STYLE="font-family: sans-serif;"
><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: An in-depth exploration of the art of shell scripting</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
><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="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><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
></TD
></TR
></TABLE
>
</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="PRIMES2"
></A
><P
><B
>Example 16-46. Generating prime numbers</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # primes2.sh
3
4 # Generating prime numbers the quick-and-easy way,
5 #+ without resorting to fancy algorithms.
6
7 CEILING=10000 # 1 to 10000
8 PRIME=0
9 E_NOTPRIME=
10
11 is_prime ()
12 {
13 local factors
14 factors=( $(factor $1) ) # Load output of `factor` into array.
15
16 if [ -z "${factors[2]}" ]
17 # Third element of "factors" array:
18 #+ ${factors[2]} is 2nd factor of argument.
19 # If it is blank, then there is no 2nd factor,
20 #+ and the argument is therefore prime.
21 then
22 return $PRIME # 0
23 else
24 return $E_NOTPRIME # null
25 fi
26 }
27
28 echo
29 for n in $(seq $CEILING)
30 do
31 if is_prime $n
32 then
33 printf %5d $n
34 fi # ^ Five positions per number suffices.
35 done # For a higher $CEILING, adjust upward, as necessary.
36
37 echo
38
39 exit</PRE
></TD
></TR
></TABLE
><HR></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="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="USERINPUT"
><B
>variable=$(echo "OPTIONS; OPERATIONS" | bc)</B
></TT
>
</PRE
></TD
></TR
></TABLE
>
</P
><P
><A
NAME="MONTHLYPMT0"
></A
></P
><DIV
CLASS="EXAMPLE"
><HR><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
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # monthlypmt.sh: Calculates monthly payment on a mortgage.
3
4
5 # This is a modification of code in the
6 #+ "mcalc" (mortgage calculator) package,
7 #+ by Jeff Schmidt
8 #+ and
9 #+ Mendel Cooper (yours truly, the ABS Guide author).
10 # http://www.ibiblio.org/pub/Linux/apps/financial/mcalc-1.6.tar.gz
11
12 echo
13 echo "Given the principal, interest rate, and term of a mortgage,"
14 echo "calculate the monthly payment."
15
16 bottom=1.0
17
18 echo
19 echo -n "Enter principal (no commas) "
20 read principal
21 echo -n "Enter interest rate (percent) " # If 12%, enter "12", not ".12".
22 read interest_r
23 echo -n "Enter term (months) "
24 read term
25
26
27 interest_r=$(echo "scale=9; $interest_r/100.0" | bc) # Convert to decimal.
28 # ^^^^^^^^^^^^^^^^^ Divide by 100.
29 # "scale" determines how many decimal places.
30
31 interest_rate=$(echo "scale=9; $interest_r/12 + 1.0" | bc)
32
33
34 top=$(echo "scale=9; $principal*$interest_rate^$term" | bc)
35 # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
36 # Standard formula for figuring interest.
37
38 echo; echo "Please be patient. This may take a while."
39
40 let "months = $term - 1"
41 # ====================================================================
42 for ((x=$months; x > 0; x--))
43 do
44 bot=$(echo "scale=9; $interest_rate^$x" | bc)
45 bottom=$(echo "scale=9; $bottom+$bot" | bc)
46 # bottom = $(($bottom + $bot"))
47 done
48 # ====================================================================
49
50 # --------------------------------------------------------------------
51 # Rick Boivie pointed out a more efficient implementation
52 #+ of the above loop, which decreases computation time by 2/3.
53
54 # for ((x=1; x <= $months; x++))
55 # do
56 # bottom=$(echo "scale=9; $bottom * $interest_rate + 1" | bc)
57 # done
58
59
60 # And then he came up with an even more efficient alternative,
61 #+ one that cuts down the run time by about 95%!
62
63 # bottom=`{
64 # echo "scale=9; bottom=$bottom; interest_rate=$interest_rate"
65 # for ((x=1; x <= $months; x++))
66 # do
67 # echo 'bottom = bottom * interest_rate + 1'
68 # done
69 # echo 'bottom'
70 # } | bc` # Embeds a 'for loop' within command substitution.
71 # --------------------------------------------------------------------------
72 # On the other hand, Frank Wang suggests:
73 # bottom=$(echo "scale=9; ($interest_rate^$term-1)/($interest_rate-1)" | bc)
74
75 # Because . . .
76 # The algorithm behind the loop
77 #+ is actually a sum of geometric proportion series.
78 # The sum formula is e0(1-q^n)/(1-q),
79 #+ where e0 is the first element and q=e(n+1)/e(n)
80 #+ and n is the number of elements.
81 # --------------------------------------------------------------------------
82
83
84 # let "payment = $top/$bottom"
85 payment=$(echo "scale=2; $top/$bottom" | bc)
86 # Use two decimal places for dollars and cents.
87
88 echo
89 echo "monthly payment = \$$payment" # Echo a dollar sign in front of amount.
90 echo
91
92
93 exit 0
94
95
96 # Exercises:
97 # 1) Filter input to permit commas in principal amount.
98 # 2) Filter input to permit interest to be entered as percent or decimal.
99 # 3) If you are really ambitious,
100 #+ expand this script to print complete amortization tables.</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
><A
NAME="BASE0"
></A
></P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="BASE"
></A
><P
><B
>Example 16-48. Base Conversion</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 ###########################################################################
3 # Shellscript: base.sh - print number to different bases (Bourne Shell)
4 # Author : Heiner Steven (heiner.steven@odn.de)
5 # Date : 07-03-95
6 # Category : Desktop
7 # $Id: base.sh,v 1.2 2000/02/06 19:55:35 heiner Exp $
8 # ==> Above line is RCS ID info.
9 ###########################################################################
10 # Description
11 #
12 # Changes
13 # 21-03-95 stv fixed error occuring with 0xb as input (0.2)
14 ###########################################################################
15
16 # ==> Used in ABS Guide with the script author's permission.
17 # ==> Comments added by ABS Guide author.
18
19 NOARGS=85
20 PN=`basename "$0"` # Program name
21 VER=`echo '$Revision: 1.2 $' | cut -d' ' -f2` # ==> VER=1.2
22
23 Usage () {
24 echo "$PN - print number to different bases, $VER (stv '95)
25 usage: $PN [number ...]
26
27 If no number is given, the numbers are read from standard input.
28 A number may be
29 binary (base 2) starting with 0b (i.e. 0b1100)
30 octal (base 8) starting with 0 (i.e. 014)
31 hexadecimal (base 16) starting with 0x (i.e. 0xc)
32 decimal otherwise (i.e. 12)" >&2
33 exit $NOARGS
34 } # ==> Prints usage message.
35
36 Msg () {
37 for i # ==> in [list] missing. Why?
38 do echo "$PN: $i" >&2
39 done
40 }
41
42 Fatal () { Msg "$@"; exit 66; }
43
44 PrintBases () {
45 # Determine base of the number
46 for i # ==> in [list] missing...
47 do # ==> so operates on command-line arg(s).
48 case "$i" in
49 0b*) ibase=2;; # binary
50 0x*|[a-f]*|[A-F]*) ibase=16;; # hexadecimal
51 0*) ibase=8;; # octal
52 [1-9]*) ibase=10;; # decimal
53 *)
54 Msg "illegal number $i - ignored"
55 continue;;
56 esac
57
58 # Remove prefix, convert hex digits to uppercase (bc needs this).
59 number=`echo "$i" | sed -e 's:^0[bBxX]::' | tr '[a-f]' '[A-F]'`
60 # ==> Uses ":" as sed separator, rather than "/".
61
62 # Convert number to decimal
63 dec=`echo "ibase=$ibase; $number" | bc` # ==> 'bc' is calculator utility.
64 case "$dec" in
65 [0-9]*) ;; # number ok
66 *) continue;; # error: ignore
67 esac
68
69 # Print all conversions in one line.
70 # ==> 'here document' feeds command list to 'bc'.
71 echo `bc <<!
72 obase=16; "hex="; $dec
73 obase=10; "dec="; $dec
74 obase=8; "oct="; $dec
75 obase=2; "bin="; $dec
76 !
77 ` | sed -e 's: : :g'
78
79 done
80 }
81
82 while [ $# -gt 0 ]
83 # ==> Is a "while loop" really necessary here,
84 # ==>+ since all the cases either break out of the loop
85 # ==>+ or terminate the script.
86 # ==> (Above comment by Paulo Marcel Coelho Aragao.)
87 do
88 case "$1" in
89 --) shift; break;;
90 -h) Usage;; # ==> Help message.
91 -*) Usage;;
92 *) break;; # First number
93 esac # ==> Error checking for illegal input might be appropriate.
94 shift
95 done
96
97 if [ $# -gt 0 ]
98 then
99 PrintBases "$@"
100 else # Read from stdin.
101 while read line
102 do
103 PrintBases $line
104 done
105 fi
106
107
108 exit</PRE
></TD
></TR
></TABLE
><HR></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
><PRE
CLASS="PROGRAMLISTING"
> 1 variable=`bc << LIMIT_STRING
2 options
3 statements
4 operations
5 LIMIT_STRING
6 `
7
8 ...or...
9
10
11 variable=$(bc << LIMIT_STRING
12 options
13 statements
14 operations
15 LIMIT_STRING
16 )</PRE
></TD
></TR
></TABLE
>
</P
><DIV
CLASS="EXAMPLE"
><HR><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
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # Invoking 'bc' using command substitution
3 # in combination with a 'here document'.
4
5
6 var1=`bc << EOF
7 18.33 * 19.78
8 EOF
9 `
10 echo $var1 # 362.56
11
12
13 # $( ... ) notation also works.
14 v1=23.53
15 v2=17.881
16 v3=83.501
17 v4=171.63
18
19 var2=$(bc << EOF
20 scale = 4
21 a = ( $v1 + $v2 )
22 b = ( $v3 * $v4 )
23 a * b + 15.35
24 EOF
25 )
26 echo $var2 # 593487.8452
27
28
29 var3=$(bc -l << EOF
30 scale = 9
31 s ( 1.7 )
32 EOF
33 )
34 # Returns the sine of 1.7 radians.
35 # The "-l" option calls the 'bc' math library.
36 echo $var3 # .991664810
37
38
39 # Now, try it in a function...
40 hypotenuse () # Calculate hypotenuse of a right triangle.
41 { # c = sqrt( a^2 + b^2 )
42 hyp=$(bc -l << EOF
43 scale = 9
44 sqrt ( $1 * $1 + $2 * $2 )
45 EOF
46 )
47 # Can't directly return floating point values from a Bash function.
48 # But, can echo-and-capture:
49 echo "$hyp"
50 }
51
52 hyp=$(hypotenuse 3.68 7.31)
53 echo "hypotenuse = $hyp" # 8.184039344
54
55
56 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
><A
NAME="CANNONREF"
></A
></P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="CANNON"
></A
><P
><B
>Example 16-50. Calculating PI</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # cannon.sh: Approximating PI by firing cannonballs.
3
4 # Author: Mendel Cooper
5 # License: Public Domain
6 # Version 2.2, reldate 13oct08.
7
8 # This is a very simple instance of a "Monte Carlo" simulation:
9 #+ a mathematical model of a real-life event,
10 #+ using pseudorandom numbers to emulate random chance.
11
12 # Consider a perfectly square plot of land, 10000 units on a side.
13 # This land has a perfectly circular lake in its center,
14 #+ with a diameter of 10000 units.
15 # The plot is actually mostly water, except for land in the four corners.
16 # (Think of it as a square with an inscribed circle.)
17 #
18 # We will fire iron cannonballs from an old-style cannon
19 #+ at the square.
20 # All the shots impact somewhere on the square,
21 #+ either in the lake or on the dry corners.
22 # Since the lake takes up most of the area,
23 #+ most of the shots will SPLASH! into the water.
24 # Just a few shots will THUD! into solid ground
25 #+ in the four corners of the square.
26 #
27 # If we take enough random, unaimed shots at the square,
28 #+ Then the ratio of SPLASHES to total shots will approximate
29 #+ the value of PI/4.
30 #
31 # The simplified explanation is that the cannon is actually
32 #+ shooting only at the upper right-hand quadrant of the square,
33 #+ i.e., Quadrant I of the Cartesian coordinate plane.
34 #
35 #
36 # Theoretically, the more shots taken, the better the fit.
37 # However, a shell script, as opposed to a compiled language
38 #+ with floating-point math built in, requires some compromises.
39 # This decreases the accuracy of the simulation.
40
41
42 DIMENSION=10000 # Length of each side of the plot.
43 # Also sets ceiling for random integers generated.
44
45 MAXSHOTS=1000 # Fire this many shots.
46 # 10000 or more would be better, but would take too long.
47 PMULTIPLIER=4.0 # Scaling factor.
48
49 declare -r M_PI=3.141592654
50 # Actual 9-place value of PI, for comparison purposes.
51
52 get_random ()
53 {
54 SEED=$(head -n 1 /dev/urandom | od -N 1 | awk '{ print $2 }')
55 RANDOM=$SEED # From "seeding-random.sh"
56 #+ example script.
57 let "rnum = $RANDOM % $DIMENSION" # Range less than 10000.
58 echo $rnum
59 }
60
61 distance= # Declare global variable.
62 hypotenuse () # Calculate hypotenuse of a right triangle.
63 { # From "alt-bc.sh" example.
64 distance=$(bc -l << EOF
65 scale = 0
66 sqrt ( $1 * $1 + $2 * $2 )
67 EOF
68 )
69 # Setting "scale" to zero rounds down result to integer value,
70 #+ a necessary compromise in this script.
71 # It decreases the accuracy of this simulation.
72 }
73
74
75 # ==========================================================
76 # main() {
77 # "Main" code block, mimicking a C-language main() function.
78
79 # Initialize variables.
80 shots=0
81 splashes=0
82 thuds=0
83 Pi=0
84 error=0
85
86 while [ "$shots" -lt "$MAXSHOTS" ] # Main loop.
87 do
88
89 xCoord=$(get_random) # Get random X and Y coords.
90 yCoord=$(get_random)
91 hypotenuse $xCoord $yCoord # Hypotenuse of
92 #+ right-triangle = distance.
93 ((shots++))
94
95 printf "#%4d " $shots
96 printf "Xc = %4d " $xCoord
97 printf "Yc = %4d " $yCoord
98 printf "Distance = %5d " $distance # Distance from
99 #+ center of lake
100 #+ -- the "origin" --
101 #+ coordinate (0,0).
102
103 if [ "$distance" -le "$DIMENSION" ]
104 then
105 echo -n "SPLASH! "
106 ((splashes++))
107 else
108 echo -n "THUD! "
109 ((thuds++))
110 fi
111
112 Pi=$(echo "scale=9; $PMULTIPLIER*$splashes/$shots" | bc)
113 # Multiply ratio by 4.0.
114 echo -n "PI ~ $Pi"
115 echo
116
117 done
118
119 echo
120 echo "After $shots shots, PI looks like approximately $Pi"
121 # Tends to run a bit high,
122 #+ possibly due to round-off error and imperfect randomness of $RANDOM.
123 # But still usually within plus-or-minus 5% . . .
124 #+ a pretty fair rough approximation.
125 error=$(echo "scale=9; $Pi - $M_PI" | bc)
126 pct_error=$(echo "scale=2; 100.0 * $error / $M_PI" | bc)
127 echo -n "Deviation from mathematical value of PI = $error"
128 echo " ($pct_error% error)"
129 echo
130
131 # End of "main" code block.
132 # }
133 # ==========================================================
134
135 exit 0
136
137 # One might well wonder whether a shell script is appropriate for
138 #+ an application as complex and computation-intensive as a simulation.
139 #
140 # There are at least two justifications.
141 # 1) As a proof of concept: to show it can be done.
142 # 2) To prototype and test the algorithms before rewriting
143 #+ it in a compiled high-level language.</PRE
></TD
></TR
></TABLE
><HR></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="variables2.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
><PRE
CLASS="PROGRAMLISTING"
> 1 echo "[Printing a string ... ]P" | dc
2 # The P command prints the string between the preceding brackets.
3
4 # And now for some simple arithmetic.
5 echo "7 8 * p" | dc # 56
6 # Pushes 7, then 8 onto the stack,
7 #+ multiplies ("*" operator), then prints the result ("p" operator).</PRE
></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"
><HR><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
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # hexconvert.sh: Convert a decimal number to hexadecimal.
3
4 E_NOARGS=85 # Command-line arg missing.
5 BASE=16 # Hexadecimal.
6
7 if [ -z "$1" ]
8 then # Need a command-line argument.
9 echo "Usage: $0 number"
10 exit $E_NOARGS
11 fi # Exercise: add argument validity checking.
12
13
14 hexcvt ()
15 {
16 if [ -z "$1" ]
17 then
18 echo 0
19 return # "Return" 0 if no arg passed to function.
20 fi
21
22 echo ""$1" "$BASE" o p" | dc
23 # o sets radix (numerical base) of output.
24 # p prints the top of stack.
25 # For other options: 'man dc' ...
26 return
27 }
28
29 hexcvt "$1"
30
31 exit</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
>Studying the <A
HREF="external.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
<SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>dc wizards</I
></SPAN
> who delight in showing off
their mastery of this powerful, but arcane utility.</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><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
></TD
></TR
></TABLE
>
</P
><P
><A
NAME="GOLDENRATIO"
></A
>
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 dc <<< 10k5v1+2/p # 1.6180339887
2 # ^^^ Feed operations to dc using a Here String.
3 # ^^^ Pushes 10 and sets that as the precision (10k).
4 # ^^ Pushes 5 and takes its square root
5 # (5v, v = square root).
6 # ^^ Pushes 1 and adds it to the running total (1+).
7 # ^^ Pushes 2 and divides the running total by that (2/).
8 # ^ Pops and prints the result (p)
9 # The result is 1.6180339887 ...
10 # ... which happens to be the Pythagorean Golden Ratio, to 10 places.</PRE
></TD
></TR
></TABLE
>
</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="FACTR"
></A
><P
><B
>Example 16-52. Factoring</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # factr.sh: Factor a number
3
4 MIN=2 # Will not work for number smaller than this.
5 E_NOARGS=85
6 E_TOOSMALL=86
7
8 if [ -z $1 ]
9 then
10 echo "Usage: $0 number"
11 exit $E_NOARGS
12 fi
13
14 if [ "$1" -lt "$MIN" ]
15 then
16 echo "Number to factor must be $MIN or greater."
17 exit $E_TOOSMALL
18 fi
19
20 # Exercise: Add type checking (to reject non-integer arg).
21
22 echo "Factors of $1:"
23 # -------------------------------------------------------
24 echo "$1[p]s2[lip/dli%0=1dvsr]s12sid2%0=13sidvsr[dli%0=\
25 1lrli2+dsi!>.]ds.xd1<2" | dc
26 # -------------------------------------------------------
27 # Above code written by Michel Charpentier <charpov@cs.unh.edu>
28 # (as a one-liner, here broken into two lines for display purposes).
29 # Used in ABS Guide with permission (thanks!).
30
31 exit
32
33 # $ sh factr.sh 270138
34 # 2
35 # 3
36 # 11
37 # 4093</PRE
></TD
></TR
></TABLE
><HR></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"
><HR><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
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # hypotenuse.sh: Returns the "hypotenuse" of a right triangle.
3 # (square root of sum of squares of the "legs")
4
5 ARGS=2 # Script needs sides of triangle passed.
6 E_BADARGS=85 # Wrong number of arguments.
7
8 if [ $# -ne "$ARGS" ] # Test number of arguments to script.
9 then
10 echo "Usage: `basename $0` side_1 side_2"
11 exit $E_BADARGS
12 fi
13
14
15 AWKSCRIPT=' { printf( "%3.7f\n", sqrt($1*$1 + $2*$2) ) } '
16 # command(s) / parameters passed to awk
17
18
19 # Now, pipe the parameters to awk.
20 echo -n "Hypotenuse of $1 and $2 = "
21 echo $1 $2 | awk "$AWKSCRIPT"
22 # ^^^^^^^^^^^^
23 # An echo-and-pipe is an easy way of passing shell parameters to awk.
24
25 exit
26
27 # Exercise: Rewrite this script using 'bc' rather than awk.
28 # Which method is more intuitive?</PRE
></TD
></TR
></TABLE
><HR></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
>
|