Work the Shell - Solve: a Command-Line Calculator Redux
Ooops! Two months ago, I started exploring how you can write a simple but quite helpful interactive command-line calculator as a shell script and ended the column with “Next month, we'll dig into useful refinements and make it a full-blown addition to our Linux toolkit. See you then!”
Unfortunately, last month, I got sidetracked with the movie The Number 23 and started another script looking at how to do numerology within the shell scripting environment. You'd think I was a typical programmer or something, being sidetracked and losing a thread by picking up another one. It reminds me of those glorious startup days from the late 1990s too, but that's an entirely different story.
Anyway, numerology can wait another month. This column, I'd like to complete the command-line calculator because, well, because it's so darn useful and simultaneously astonishing that there isn't a decent command-line calculator in Linux after all these years. I mean, really!
It was a while back, so let me remind you that the wicked short script to give you the rudimentary calculator is this:
#!/bin/sh bc << EOF scale=4 $@ quit EOF
That's it. Name it solve.sh, for example, and you can test it, as shown here:
$ sh solve.sh 1+3 4 $ sh solve.sh 11/7 1.5714
It's easy enough to alias solve to the shell command too:
alias solve="sh solve.sh"
alias solve="sh ~/bin/solve.sh"
As that'll work regardless of where you are in the filesystem (location-dependent commands are a typical shell gaffe).
What I'd really like, however, is to be able to go into a “solve” mode where anything I type automatically is assumed to be a mathematical equation, rather than have to type solve each time.
We've talked about shell script wrappers in the past, so you should recall this basic structure:
while read userinput do echo "you entered $userinput" done
That's too crude to use as of yet, but we easily can add a prompt so that it looks like a real program:
echo -n "solve" while read expression do echo "you entered $expression" echo -n "solve: " done
Look good? Actually, it's not. There's a subtle error here, one that's another common scripting mistake. The problem is that there are two echo commands in Linux: one that's the built-in capability of the shell itself, and one that's a separate command located in /bin. This is important because the built-in echo doesn't know what the -n flag does, but the /bin/echo command does. A tiny tweak, and we're ready to test it:
/bin/echo -n "solve: " while read expression do echo "you entered $expression" /bin/echo -n "solve: " done
Let's see what happens:
solve: 1+1 you entered 1+1 solve: ^D
That's more like it.
What we really want though, is a script that's smart enough to recognize whether you've specified parameters on the command line. If you have, it solves that equation, and if you haven't, it drops you into the interactive mode.
That's surprisingly easy to accomplish by testing the $# variable, which indicates how many arguments are given to the script. Want to see if it's greater than zero? Do this:
if [ $# -gt 0 ] ; then
One more refinement before I show you the script in its entirety: I want to have it quit if users type in quit or exit, rather than force them to type ^D to indicate end of file on standard input (which causes the read statement to return false and the loop to end).
This is done with a simple string comparison test, which you'll recall is done with = (the -eq test is for numeric values). So, testing $expression to see whether it is “quit” is easy:
if [ $expression = "quit" ] ; then
To make it a bit more bulletproof, it's actually better here to quote the variable name, so that if users enter a null string (simply press Return), the conditional test won't fail with an ugly error message:
if [ "$expression" = "quit" ] ; then
Because I like to make my scripts flexible, I've also added exit as an alternative to quit, which easily is done with a slightly more complicated conditional test:
if [ "$expression" = "quit" -o "$expression" = "exit" ] ; then
The -o is the logical OR statement in a shell conditional test, but I have a feeling you've already figured that out.
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.
Practical Task Scheduling Deployment
One of the best things about the UNIX environment (aside from being stable and efficient) is the vast array of software tools available to help you do your job. Traditionally, a UNIX tool does only one thing, but does that one thing very well. For example, grep is very easy to use and can search vast amounts of data quickly. The find tool can find a particular file or files based on all kinds of criteria. It's pretty easy to string these tools together to build even more powerful tools, such as a tool that finds all of the .log files in the /home directory and searches each one for a particular entry. This erector-set mentality allows UNIX system administrators to seem to always have the right tool for the job.
Cron traditionally has been considered another such a tool for job scheduling, but is it enough? This webinar considers that very question. The first part builds on a previous Geek Guide, Beyond Cron, and briefly describes how to know when it might be time to consider upgrading your job scheduling infrastructure. The second part presents an actual planning and implementation framework.
Join Linux Journal's Mike Diehl and Pat Cameron of Help Systems.
Free to Linux Journal readers.View Now!
|The Firebird Project's Firebird Relational Database||Jul 29, 2016|
|Stunnel Security for Oracle||Jul 28, 2016|
|SUSE LLC's SUSE Manager||Jul 21, 2016|
|My +1 Sword of Productivity||Jul 20, 2016|
|Non-Linux FOSS: Caffeine!||Jul 19, 2016|
|Murat Yener and Onur Dundar's Expert Android Studio (Wrox)||Jul 18, 2016|
- The Firebird Project's Firebird Relational Database
- Stunnel Security for Oracle
- My +1 Sword of Productivity
- Non-Linux FOSS: Caffeine!
- SUSE LLC's SUSE Manager
- Managing Linux Using Puppet
- Murat Yener and Onur Dundar's Expert Android Studio (Wrox)
- Parsing an RSS News Feed with a Bash Script
- Google's SwiftShader Released
- Doing for User Space What We Did for Kernel Space
With all the industry talk about the benefits of Linux on Power and all the performance advantages offered by its open architecture, you may be considering a move in that direction. If you are thinking about analytics, big data and cloud computing, you would be right to evaluate Power. The idea of using commodity x86 hardware and replacing it every three years is an outdated cost model. It doesn’t consider the total cost of ownership, and it doesn’t consider the advantage of real processing power, high-availability and multithreading like a demon.
This ebook takes a look at some of the practical applications of the Linux on Power platform and ways you might bring all the performance power of this open architecture to bear for your organization. There are no smoke and mirrors here—just hard, cold, empirical evidence provided by independent sources. I also consider some innovative ways Linux on Power will be used in the future.Get the Guide