For loops in bash

Iteration: it’s such a powerful tool. Automating the repetition of tasks can save so much time and keystrokes and tedium. This capability is one of the reasons I love bash, my command shell of choice. I used to employ tcsh, but switched to bash years ago mainly because most linux system scripts use bash, and I wanted to just focus on one syntax so that my own scripts and the system ones would be equally readable. But bash also opened my eyes to the beauties of command-line for loops. (Tcsh makes them interactive, which initially seems nice, but then is annoying if you’d like to re-do an earlier loop you typed in.)

So now I regularly use for loops at the command line for all sorts of things: resizing a collection of images, creating a collection of symbolic links, renaming a series of files by some standard convention. It’s great for any task in which you want to do the same operation, but the individual commands differ slightly, and in which you can’t just pass in a wildcard list of inputs. But for the longest time, I thought there was only one form of the loop syntax:

for i in choice1 choice2 … choiceN ; do
… [some task with $i, the current choice]

Thanks to this great collection of for loop examples, I’ve learned that bash now also supports sequence iteration! So you can do

for i in {1..5}; do
… [some task with $i, a number from 1 to 5] …

Previously, I would have written

for i in 1 2 3 4 5 ; do
… [some task with $i, the current choice] …

which gets very clunky with larger ranges, involving nested loops, one per digit. Ugh!

Newer bashes (version 4.0+) even support increments, e.g.

for i in {1..5..2}; do
… [some task with $i, an odd number from 1 to 5] …

And wow, there’s even a C-style syntax:

for (( i=1; i<=5; i++ )); do
… [some task with $i, a number from 1 to 5] …

For loops just got easier.

I used this today to generate a file that contained 16 1’s followed by 16 2’s followed by 16 3’s… all the way up to 16 30’s. Exactly the kind of thing I don’t want to do manually. And yes, I actually needed this file in order to get some research done. So it goes!

Kinematics and engineering

OSU offers a course called ME 412: Design of Mechanisms. Sadly, it is only offered during the winter term, and I am here for the fall term. So I contacted the professor to find out more about what sort of books and other materials the course uses, for my own investigation. Imagine my delight when he gave me a copy of the latest edition of the textbook and sent me on my way!

This textbook, Design of Machinery, may well be the best textbook I’ve ever read. Really. Unlike some books that pay lip service to being clear and accessible, this text really is clear and accessible. It is also annotated with lots of amusing cartoons and very clearly illustrated examples — the latter being crucial since we’re talking about physical devices and how they move. The book comes with a DVD with animations that I haven’t been able to play with, since it is Windows-only. But, of course, there are linkage animations galore online. And even better is building them yourself, physically.

Chapter 1, “Kinematics of Mechanisms,” is charmingly written and touches on the broader subject of what it means to be an engineer, and how one of the biggest challenges is learning how to “structure the unstructured problem” to go from a concept to a problem definition to a solution. One tip the book offers is to use “functional visualization” (meaning, focus on the desired behavior of the solution, but no one kind of “embodiment”) so as not to limit your creativity and be restricted by a specific kind of solution early on. It also encourages you to make cardboard models of linkages that you design (as above) — a philosophy I think makes a lot of sense.

The chapter also includes the text from a paper given by George A. Wood Jr. titled “Educating for Creativity in Engineering.” Two of my favorite quotes from the paper are, “To me, the creative moment is the greatest reward that the profession of engineering gives,” and “If a person decides to be a designer, his training should instill in him a continuing curiosity to know how each machine he sees works.” Yes! Yes!

On a more concrete level, I learned that kinematics is “the study of motion without regard to forces,” while kinetics is “the study of forces on systems in motion.” The first half of this book focuses on kinematics, reserving forces for the second half. I can already tell that kinematics alone provides ample material to keep me busy!

“Once you become familiar with the terms and principles of kinematics, you will no longer be able to look at any machine or product without seeing its kinematic aspects.”

It’s already happened. At a conference last week, after ironing a shirt in my hotel room, I sat down and drew my first kinematics diagram — of the ironing board.

I’ve already started working through Chapter 2, “Kinematics Fundamentals,” which is fascinating but slower going, as it’s dense with new information, terms, and concepts for me. But I’m already impatient to get to the end of this chapter, where there are 15 pages of problems to do! Starting with… drawing kinematics diagrams of familiar objects like ironing boards. Ooops, jumped the gun on that one!

Astrotagging and Milky Way orbits

I attended an excellent talk today by David Hogg, a cosmologist at NYU, that was titled “A Comprehensive Model of All Astronomical Imaging Ever Taken.” Here I highlight just two of the interesting and thought-provoking topics that appeared in his talk.

Astrotagging: His group developed, a service that will analyze digital images of the night sky and automatically annotate them with identifiable stars. You can access this impressive service by uploading a photo to flickr and adding it to the group “astrometry,” as in this example. Automatically, analyzes all new images added to this group and adds a comment with all of the stars that were found, as well as marking them on the image itself. Clever, reliable, and useful! Nice work!

Milky Way orbits: Kepler deduced planetary orbits based on repeated observations of planetary positions. We know that our Sun, and the rest of the stars in our galaxy, also orbit around the Milky Way’s core. But those orbits are perturbed by the presence of dark matter, something we can’t observe directly, and anyway, it would take hundreds of millions of years just to observe one orbit. Could there be a short cut? If you look up at the sky in the right location you can find a “stream” of stars that mark out one such orbital tracks, where clumps of stars formed together but moved slightly apart, along their shared orbit. Hogg and colleagues came up with a 6-D description of an orbit fit to those observations, concluding that the best fit is “an eccentric orbit in a flattened isothermal potential.” For more details, see their paper: “Constraining the Milky Way Potential with a Six-Dimensional Phase-Space Map of the GD-1 Stellar Stream”. I wonder what this line of inquiry will end up telling us about that dark matter distribution?

Von Neumann’s first computer program

What would you do with a brand-new computer of unprecedented capabilities? Ada Lovelace demonstrated the theoretical capabilities of Babbage’s Analytical Machine with a program to compute Bernoulli numbers (1843). J. Presper Eckert and John Mauchly’s first program for ENIAC was a ballistics calculation (1946). Alan Turing used the Manchester Mark 1 to compute Mersenne primes (1949). When John von Neumann had the chance to explore the abilities of ENIAC’s successor, EDVAC, he focused on something quite different: data sorting.

Donald Knuth wrote a fascinating 1970 paper, “Von Neumann’s First Computer Program,” which analyzes the original 23-page handwritten document where von Neumann recorded his planned program. Von Neumann conceived of a merge sort algorithm and wrote out an implementation of the merge part of it, which Knuth examined and reported on. Knuth noted the insights von Neumann achieved and then respectfully, humbly, pointed out places where von Neumann’s code could be optimized as well as an unfortunate bug in the program. As Knuth noted, not bad for programming without having the actual machine yet to test it! (I do not know how Knuth found the error, whether by hand-examination or by running the program through a simulator.)

There were some very pressing reasons why ballistics and numerical simulations were the top priorities for the electronic computers just coming into existence in the late 1940’s. Von Neumann’s foray into “a nonnumeric application for computers” foreshadowed the generation and growth of information processing and a landslide of other associated activities and applications in use throughout the world today.

Ingenuity Engines

I’m John Lienhard, at the University of Houston, where we’re interested in the way inventive minds work.

Well, I’m not John Lienhard, but I’m delighted to make his acquaintance. I just discovered the excellent podcast, “Engines of Our Ingenuity”, which “tells the story of how our culture is formed by human creativity.” Each ~4-minute episode covers some interesting concept, device, invention, inventor, or tidbit of history that bears on our world today. The first one that came up when I subscribed was on The Calculus—and the delightfully accessible and meaningful way in which this subject was covered won me over immediately. Calculus, the mathematics of change, is described as “not any harder than algebra,” just having “quite a different look and feel.”

A beautiful moment in this episode is when Dr. Lienhard connects the concept of calculus to our own lives:

We all see our lives as fleeting moments and as the sum of fragments almost too small to notice.

and then proceeds to quote Joan Didion (Run, River):

“Was there ever in anyone’s life span a point free in time, devoid of memory, a night when choice was any more than the sum of all the choices gone before?”


This show has been going on since 1988. What a wealth of archives to explore! I very much look forward to learning more from Dr. Lienhard about the basis of inventions, and I’m grateful that people out there take the time to create such impressive, useful, fascinating content!