| |
Unix Tip: More on bc
ITworld 03/20/2008
Sandra Henry-Stocker, ITworld.com
In a previous column, we looked at bc, the surprisingly high
precision and big value capable Unix tool that you will find
on most versions of Unix today. The bc utility is not just a
command line tool, however. You can also write numerically
intensive scripts in bc. The tool incorporates enough syntax
to assign values to variables, write and call functions,
collect responses from users and print annotated results.
To start a bc script, you include the path to bc in your
shebang line as you would for any shell.
Let's look at a script for computing factorials. The main
part of this script (the "f" function) comes straight from the
man page. I've added a -q to the shebang line so that the
script doesn't print the traditional bc welcome line, a prompt
to nudge the user into providing some input, a read statement
to collect the user's response, a call to the f function, a
command to print the result and a quit command (otherwise, you
will still be running bc when the script has ended.
#!/usr/bin/bc -q
/* factorial */
define f (x) {
if (x <= 1) return (1);
return (f(x-1) * x);
}
print "num> "
num=read()
ans=f(num)
print ans
print "\n"
quit
|
Notice that function f is recursive. Based on our understanding
of factorials, each time the function calls itself, it does so
with a decremented argument. If the user enters 3, for example,
the function is called from the main body of the script with the
argument 3 and the function then calls itself twice -- once with
the number 2 and once with the number 1.
Notice the C-like comment on line 3 and the print newline statement
on line 13. Like C and Perl, you need to request a newline if you
want one. Even if you are using bc interactively, you will not
get a newline unless you ask for one. Type "print 11" and your
cursor will be sitting to the right of the printed number.
Probably the hardest thing to get used to is the lack of a prompt.
You type "bc" on the command line and you will see something like
this:
rivet> bc
bc 1.05
Copyright 1991, 1992, 1993, 1994, 1997, 1998 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
|
Your cursor then sits on a blank line while bc waits for your request.
If we follow the math through the script, we see that f(3) becomes
f(2) * 3 which becomes f(1) * 2 * 3 which finally becomes 1 * 2 * 3
or 6.
If the user types in a large number, say 111 or 1234, the result
is going to span multiple lines just as did many of the answers
we looked at last week:
rivet> ./factorial
num> 111
176295255109024466387216104710707578876140953602656551604157406\
33473469550872483164365555745984623157731960476628379789131458474971\
99871623320096254145331200000000000000000000000000
|
The bc utility also includes a rudimentary form of test logic. If
you type "12 > 6" in bc, for example, bc will respond with a "1",
meaning "true"; Twelve is greater than 6. If you type "6 > 12" on
the other hand, the response will be "0" for false. In a similar
vein, you can use less than (<), less than or equal (<=), greater
than or equal (>=), equal (==) and not equal (!=). You can also
calculate the reverse response, if for some reason you want to, by
prepending a not (!) sign to your statement. For example:
AND and OR operations are invoked with || and && operators as shown
in these examples:
(6>12)||(6>10)
0
(6>5)&&(6>3)
1
|
The bc utitity also has while and for statements. Here's a simple
example of while statement:
while ( number > 0 ) {
print number
number=number-1
}
1110987654321
|
Here's a similar for statement:
for (number=11; number=number-1; number>0) {
print number
print "\n"
}
10
9
8
7
6
5
4
3
2
1
|
Some versions of bc also provide geometric functions, such as sine
and cosine and natural logarithms.
There's a lot to this little language you are likely to find it handy
whether you need to work in various numeric bases, handle extremely
large numbers or do rapid fire numerical comparisons.
Sandra Henry-Stocker has been administering Unix systems
for more than 18 years. She describes herself as "USL"
(Unix as a second language) but remembers enough English
to write books and buy groceries. She
currently works for TeleCommunication Systems, a wireless
communications company, in Annapolis, Maryland, where no
one else necessarily shares any of her opinions. She lives
with her second family on a small farm on Maryland's
Eastern Shore. Send comments and suggestions to bugfarm@gmail.com.
|
|
|