Work the Shell - Solve: a Command-Line Calculator Redux

 in
Dave completes his explanation of writing a helpful interactive command-line calculator as a shell script.
The Full Script

Here's where the script stands at this point, in its entirety:


#!/bin/sh

if [ $# -gt 0 ] ; then
bc << EOF
scale=4
$@
quit
EOF
else
  /bin/echo -n "solve: "

  while read expression
  do
    if [ "$expression" = "quit" -o 
         "$expression" = "exit" ] ; then
      exit 0
    fi
bc << EOF
scale=4
$expression
quit
EOF
    /bin/echo -n "solve: "
  done

  echo ""
  echo "solved."
fi

exit 0

Neat and darn useful, I'd say. If I were to continue hacking on it, the next thing I would do is write a simple help page that I'd store in some library folder and display on entry of ? or help. It simply would explain the syntax of the expressions understood by bc (though as we're invoking bc iteratively, we can't have persistent variables and so forth, so unfortunately, this approach won't let us access the full power of the binary calculator).

To learn what type of sophisticated expressions you can enter, simply type man bc. Then, let that be your inspiration for further tweaks and mods to this script!

Next month, I'll go back to the numerology script and see what strange things we can ascertain about the apparently benign world around us. Remember, just because I got out of sequence, doesn't mean we're not out to get you, dear reader!

Dave Taylor is a 26-year veteran of UNIX, creator of The Elm Mail System, and most recently author of both the best-selling Wicked Cool Shell Scripts and Teach Yourself Unix in 24 Hours, among his 16 technical books. His main Web site is at www.intuitive.com, and he also offers up tech support at AskDaveTaylor.com.

______________________

Dave Taylor has been hacking shell scripts for over thirty years. Really. He's the author of the popular "Wicked Cool Shell Scripts" and can be found on Twitter as @DaveTaylor and more generally at www.DaveTaylorOnline.com.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

I use this great command

Mike 2009's picture

I use this great command line calculator

http://www.fnoware.st/?CLC-linux

There is a windows version also that I use on my windows computer

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix