Zero Padding in Bash

April 30th, 2007 § 32 comments § permalink

This is as much a reminder for myself as anything. I needed to create 31 folders named 00 through 31 for each day of the month in a web-directory used to store audio files from CAPE-1 transmissions. The first thought that came to mind was to write a quick one-liner bash command/script to create those folders. So, without much thought, I quickly typed the following into a shell on my web server:

for ((x=1;x<=31;x+=1)); do mkdir $x; done

OK, well that did create 31 directories, but the first 9 were named 1, 2, 3, etc. instead of 01, 02, 03 like I wanted. After a quick search on zero-padding in bash it seemed no one had posted anything about it except to say it was tricky. Being a C programmer before a bash script writer, I thought to myself, “Wouldn’t it be nice if I could use a printf() statement in bash?” Then I remembered that you can, only usually it is used for formatting user interface statements. I could not think of any reason it would not work to deliver the argument to a mkdir statement, so I tried:


$ for ((x=1;x< =31;x+=1)); do mkdir `printf "%02d" $x`; done
$ ls
01 03 05 07 09 11 13 15 17 19 21 23 25 27 29 31
02 04 06 08 10 12 14 16 18 20 22 24 26 28 30

And it works! Breaking the command down gives a standard bash for loop with the mkdir command as the internal command inside the loop. As an argument to the mkdir command, however, is a printf statement which given the formatting prints a zero-padded (the 0 after %) two-character long (the 2 after the 0) integer, x. Remember, the `s are back-ticks (below the tilde on most keyboards), not single quotation marks (‘).

It seems like there should be a better way of doing this, but quickest way I found to add zero padding to a number in bash is to use the provided printf which uses the same formatting as the C language printf statement. More usage and examples are available at the ss64.org bash printf reference page.

Our Day Off in California

December 11th, 2006 § Comments Off on Our Day Off in California § permalink

Nick and I had the day off since CAPE1’s testing went so well. We decided to take a drive up the coast and snapped these photos.

Dumpster DivingFishing Boat at Morro BayFishing Boat at Morro BaySeagull at Morro Bay, CAElephant Seal North of Morro Bay, CA

There’s a short video of the seal at YouTube.

CAPE1 Thermal Vacuum Test Photos

December 10th, 2006 § Comments Off on CAPE1 Thermal Vacuum Test Photos § permalink

After ten hours of a grueling vacuum bake out, CAPE1 passed the thermal vacuum test without a hitch. Here are some photos of the event.

Unpacking CAPE1 for Thermal Vacuum TestingUnpacking CAPE1 for Thermal Vacuum TestingUnpacking CAPE1 for Thermal Vacuum TestingCAPE1 Ready for Thermal VacuumCAPE1 Ready for Thermal VacuumThermal Vacuum Waiting Ready for CAPE1CAPE1 Inside the Thermal VacuumCAPE1 Inside the Thermal VacuumSealing the Thermal Vacuum
CAPE1 Inside the Thermal Vacuum after TestingRemoving the Temperature Probes from CAPE1Removing CAPE1 from the Transportation Packaging for Diagnostic TestingSecuring the Diagnostic Cable to CAPE1Commanding CAPE1 through the Diagnostic BoxEverything Seems OK
Everything is Working Perfectly

CAPE1 Fit Check and Vibration Test Photos

December 8th, 2006 § Comments Off on CAPE1 Fit Check and Vibration Test Photos § permalink

Nick and I are here at Cal Poly in San Luis Obispo, CA for final qualification tests of CAPE1. Here are some photos:

Unpacking CAPE1 from the Test PodUnpacking CAPE1 from the Test PodWeighing CAPE 1 (It weighed 879g)CAPE1 Fit CheckCAPE1 Fit CheckCAPE1 Fit CheckCAPE1 on the Vibration TableCAPE1 on the Vibration TableCAPE1 on the Vibration TableCAPE1 on the Vibration TableTaking CAPE1 Out of the P-POD after Vibration TestingExamining CAPE1 after Vibration TestingPerforming the Shake CheckPlugging CAPE1 into the Diagnostic BoxIt Works!Sealing CAPE1 for Thermal Vacuum Tests TomorrowSealing CAPE1 for Thermal Vacuum Tests Tomorrow

CAPE1 Solar Panel Photos

July 21st, 2006 § Comments Off on CAPE1 Solar Panel Photos § permalink

Shawn and Blaise glued the solar cells on the Z+ and Z- solar panels of CAPE1. They actually did this last week, but I’ve just now uploaded the photos.

CAPE1 Solar Panels on the Stencil BackplateDo Not Touch Reflection in CAPE1 Solar PanelsClose Up of One of the CAPE1 Solar PanelsMacro Shot of One of the CAPE1 Solar PanelsAnother Macro Shot of One of the Cells. Another Macro Shot of One of the Cells. CAPE1 Solar Panels on the Stencil BackplateCAPE1 Solar Panel with Tabs Soldered

Working with the CAPE Subversion System

June 18th, 2006 § Comments Off on Working with the CAPE Subversion System § permalink

The CAPE Subversion System serves as both a version control system and a central repository for important design documents and software. The basis of this system is the database driven Subversion (sometimes referred to as SVN) software. Subversion provides both the host and client software for creating and accessing a local or remote repository. The Subversion host software automatically handles version incrementing as well as maintaining a history of the repository. Need to take a look at a file from three days ago that has been changed seven times since? Subversion can handle it.

» Read the rest of this entry «

Where Am I?

You are currently browsing the C.A.P.E. category at Jonathan Wagner.