Mac in the Shell

Back to bash Basics: Following Up on the bash Presented Thus Far

by Edward
Marczak

Carpenters shape wood. Fletchers shape arrows. Programmers shape code. So far, we've been
apprentices in the mastery of bash scripting. This month, we'll take another step up that ladder,
and learn how to make our code flow. In the ever-blurring distinction between programming and
scripting, let's look at some of the bash shell's better programming conventions and statements.

Too smart for me

I'm going to make several assumptions this month. Namely:

You know how to turn on a computer

You've been following my column for a little bit

You know what variables are

You're smart, and have been around this stuff for at least a little bit

You've seen some kind of flow control statements in some language, somewhere

Not too rough, right? Good. Let's go.

Why are you beating me?

Since bash is built-in to every OS X box, present and ready-to-go, it's a great tool for small,
quick and dirty scripts, to more complex and full-featured (dare I say?) applications. While
"application" may be a stretch, for any code to approach that, it has to be able to make decisions.
The scripting 'techniques' presented thus far have really been pouring the foundation, which is
important, but have really been little more than stringing commands together sequentially. Fine for
basic automation, but limited in the grand scheme of things.

We need a way to introduce flow control into our code. This is a basic concept in all
programming languages. Flow control is the classic, "if some condition exists, then follow these
instructions." Or, "perform this set of statements x number of times." Of course, the syntax tends
to be a little different between each language, and bash won't let you down. First, here's what
bash gives us:

if/else

while/until

for

case

select

Why are you beating me?

if/else is about as basic as it gets. When you need to decide if your code should, or should not
do something, you use if/else. You've probably seen this before - even Excel's VBA has if tests!
Here's where bash is different: most languages test for some true Boolean condition. bash's if
tests exit codes. That's it. Lodge that in your brain and you'll save yourself grief later on.

Every Unix program returns a code, in the form of an integer in the range 0-255, to its parent
process when it quits - the exit code. Here's where your brain may spin: 0 is (typically) the "OK"
code, meaning success. So, in an if statement, an exit code of "0" means, "true, run this code."
However, this is a de facto standard, and the meanings of exit codes are up to the programmer.

A better way to think of this is that of "exit success." So:

if some command was successful thendo this bit of code elseyell and scream fi

bash uses the variable "?" to store exit status of the most recently run program. To illustrate,
follow this example:

$ touch example.txt
$ echo $?
0
$ grep asdf example.txt
$ echo $?
1

"?" shows the exit codes. "touch example.txt" was successful; it returned a "0" exit code. grep,
however, couldn't find the string "asdf" in example.txt, so it was unsuccessful, and handed us back
a "1" exit code. Of course, we can use this in a script.

ping, like most utilities, will hand us back a "0" on success, and some value greater than zero
if there's a problem. Here's a cheap-o host monitor script:

We try to ping a host at 4.2.2.2 (and if you're connected to the net, this should work). If the
ping succeeds, we simply make a note of it. If the ping fails, we send mail to alert us to that
fact, and run another script that pages a tech.

We can also test two conditions at a time with the "&&" (and) and "||" (or) operators. If we
needed two pings to different hosts to succeed, we could use "&&". Changing our if line to read:

if ping 130.57.4.27 && ping 4.4.4.2

will run the first statement. Only if it succeeds do we even try the second. Both the first and
the second statement must succeed to enter the "then" section of the if statement. You can do
something similar outside of a script:

copyfile.sh && alterfile.sh && ftpfile.sh

This example will run three scripts in succession. The next in the series will only run if the
previous script exited with a successful exit code.

The "||" operator runs the first statement, if it succeeds, statement 2 never runs, and we enter
the then clause. If statement 1 fails, statement 2 does run. If it succeeds, we enter the then
clause. Either statement's success will get us into the then clause. Consider revising our
example:

if ping 130.57.4.27 || ping 4.2.2.2

Perhaps we're just trying to see if this machine is connected to the Internet. Say host
130.57.4.27 goes down. As long as we can still ping 4.4.4.2, this script doesn't send mail to us,
or try to page someone.

if/then may be basic, but just this simple decision scheme alone can create powerful scripts.
Let's make it more so. if/then is powerful, but testing exit codes alone can get tedious. The
shell provides the test command to test various conditions. To make the command more concise and
visually pleasing, [ is aliased to test. So this:

if test -e /bin/bash

is the same as this:

if [ -e /bin/bash ]

Most people use the [...] variant. I will do the same. When I say, "test", I'm referring to test
or [. What can test check for us? The man page is of great use here, but I'll give an example or
two:

File tests:

-e test for a file's existence, no matter the type.

-d test for a directory

String comparisons:

s1 = s2 string 1 is equal to string 2.

-n string is not
null

Integer comparisons:

-lt less than

-eq equal

-ne not equal

Other notes for inside the brackets: you can use "!" to negate a test:

If the "reports" directory or the nightly file doesn't exist, we try to rsync them. If that goes
well, success all around. If the directory or file do exist, we're done. If the rsync fails, we
note it in the system log. That packs a lot in a single "if" statement.

Why are you beating me?

If you're an old Pascal or C hand, you'll recognize these two constructs: while and until. For
the uninitiated, while repeats a block of statements for the duration of a condition being true.
until runs a block of code until a condition is met. One look at them in action, and you'll get it:

while [ -e /run/statusflag.txt ]
do
copyfiles.sh
sleep 300
done

The until block is very similar:

until [ -e thecowscomehome.txt ]
do
jump_over_moon.sh
done

Easy, right? Naturally, you can run all of the tests that the if statement allows.

Why are you beating me?

Sometimes, either you or a variable know how many times a loop should run. (Does that make
anyone else think of Tron?). The constructs of for and forelse allow us to do just this. This is
where you'll see a lot of difference from traditional languages. While you can pull off those kinds
of loops:

#!/bin/bash
for ((i=1;i<=10;i+=1))
do
echo $i
done

...you'll very rarely see that done in practice. What happens to be much cooler than this, though,
is the ability to loop through file lists, directories and command line arguments passed to your
script. This is truly, as the kids say, da bomb. Look at this simple example:

Well, gosh, I could have simply used the one-line echo *.sh to achieve the same thing. How about
something that gives us info on each file? Line by line output can be achieved in a few different
ways, but here's the way I'm choosing, for the moment:

#!/bin/bash
FILES=`ls *.sh`
for i in $FILES
do
echo $i
if [ -O $i ]; then
echo "You own $i."
fi
if [ -G $i ]; then
echo "You are a group owner of $i."
fi
echo
done

Running the above gives me something akin to this:

$ ./loopy2.sh
backup.sh
You own backup.sh.
You are a group owner of backup.sh.
createdmg.sh
You own createdmg.sh.
fctest.sh
You own fctest.sh.
it.sh
You own it.sh.
Loopy2.sh
You own loopy.sh.
You are a group owner of loopy2.sh

As mentioned, for is a fantastic way for dealing with arguments passed into the script. Here's a
slightly contrived example that will make a directory and copy the files and/or types (based on
extension) into that directory. The new concept here are the parameter variables: @ and #. "$@"
contains a list of each positional parameter, $1, $2...$N that is passed into the script. "$#"
contains the number of parameters passed in. We can even include a little error correction this
way:

Why are you beating me?

The last flow control statements we'll cover this month, case and select, bring their own power
to bash, much like the previous flow control variants.

case is a neat alternative to a bevy of if/then/else statements. You immediately know the reason
for this if you've used "case" in Pascal, or "switch" in C. If you've never seen those, one example
and you'll be a pro:

Save this in a new file, make it executable and pass it some files you want information about. I
really think the way you can process multiple options on one line is really elegant.

select is really its own unique construct. It will take the list of items passed to it, create a
numbered menu out of those lists and wait for input. Watch it in action (file gives us information
about a file, as seen on line 5):

A bit Spartan, perhaps, but certainly functional, and will save you a ton of work. Again, the
power here lies in being able to build menus, completely ad-hoc, based on the contents of a
directory.

It's all about your style

Next month, we'll get a little deeper into why some of the above works, as we probe deeper into
the bowels of bash. Also, we'll expand on other loop constructs. Like any programming, there's
typically more than one way to achieve the same thing. You can avoid all use of until by negating a
while loop. You can split up lists with bash's processing, grep, sed, cut, and other utilities. I
can only serve as a guide. Practice, err, correct; then the apprentice becomes the master.

Ed Marczak, owns and operates Radiotope, a technology consulting company that specializes
in networking, workflow automation, teaching about technology, and helping out. Tech relief at
http://www.radiotope.com

Community Search:

MacTech Search:

Software Updates via MacUpdate

EtreCheck 3.1.5 - For troubleshooting yo...

EtreCheck is an app that displays the important details of your system configuration and allow you to copy that information to the Clipboard. It is meant to be used with Apple Support Communities to... Read more

WALTR 2 2.0.8 - $39.95

WALTR 2 helps you wirelessly drag-and-drop any music, ringtones, videos, PDF, and ePub files onto your iPhone, iPad, or iPod without iTunes. It is the second major version of Softorino's critically-... Read more

Carbon Copy Cloner 4.1.12 - Easy-to-use...

Carbon Copy Cloner backups are better than ordinary backups. Suppose the unthinkable happens while you're under deadline to finish a project: your Mac is unresponsive and all you hear is an ominous,... Read more

Dropbox 16.3.27 - Cloud backup and synch...

Dropbox is an application that creates a special Finder folder that automatically syncs online and between your computers. It allows you to both backup files and keep them up-to-date between systems... Read more

Microsoft OneNote 15.29 - Free digital n...

OneNote is your very own digital notebook. With OneNote, you can capture that flash of genius, that moment of inspiration, or that list of errands that's too important to forget. Whether you're at... Read more

Spotify 1.0.44.100. - Stream music, crea...

Spotify is a streaming music service that gives you on-demand access to millions of songs. Whether you like driving rock, silky R&B, or grandiose classical music, Spotify's massive catalogue puts... Read more

SpamSieve 2.9.27 - Robust spam filter fo...

SpamSieve is a robust spam filter for major email clients that uses powerful Bayesian spam filtering.
SpamSieve understands what your spam looks like in order to block it all, but also learns what... Read more

VueScan 9.5.62 - Scanner software with a...

VueScan is a scanning program that works with most high-quality flatbed and film scanners to produce scans that have excellent color fidelity and color balance. VueScan is easy to use, and has... Read more

Fantastical 2.3.2 - Create calendar even...

Fantastical 2 is the Mac calendar you'll actually enjoy using. Creating an event with Fantastical is quick, easy, and fun:
Open Fantastical with a single click or keystroke
Type in your event... Read more

PCalc 4.4.4 - Full-featured scientific c...

PCalc is a full-featured, scriptable scientific calculator with support for hexadecimal, octal, and binary calculations, as well as an RPN mode, programmable functions, and an extensive set of unit... Read more

Latest Forum Discussions

Christmas is fast approaching and that means it's time to prepare for Santa's yearly pilgrimage around the globe. Christmas Eve is an exciting time as parents help their kids get ready to welcome Santa. You've got the cookies and milk all planned... | Read more »

Galaxy on Fire 3 and four other fantasti...

Galaxy on Fire 3 - Manticore brings the series back for another round of daring space battles. It's familiar territory for folks who are familiar with the franchise. If you've beaten the game and are looking to broaden your horizons, might we... | Read more »

The best apps for your holiday gift exch...

What's that, you say? You still haven't started your holiday shopping? Don't beat yourself up over it -- a lot of people have been putting it off, too. It's become easier and easier to procrastinate gift shopping thanks to a number of apps that... | Read more »

MyTona, based in the chilly Siberian city of Yakutsk, has brought a little festive fun to its hidden object game Seekers Notes: Hidden Mystery. The Christmas update introduces some new inhabitants to players, and with them a chance to win plenty of... | Read more »

PINE GROVE 1.0
Device: iOS Universal
Category: Games
Price: $1.99, Version: 1.0 (iTunes)
Description:
A pine grove where there are no footsteps of people due to continuous missing cases. The case is still unsolved and nothing has... | Read more »

Niantic teases new Pokémon announcement...

After rumors started swirling yesterday, it turns out there is an official Pokémon GO update on its way. We’ll find out what’s in store for us and our growing Pokémon collections tomorrow during the Starbucks event, but Niantic will be revealing... | Read more »

3 reasons why Nicki Minaj: The Empire is...

Nicki Minaj is as business-savvy as she is musically talented and she’s proved that by launching her own game. Designed by Glu, purveyors of other fine celebrity games like cult favorite Kim Kardashian: Hollywood, Nicki Minaj: The Empire launched... | Read more »

Clash of Clans is getting its own animat...

Riding on its unending wave of fame and success, Clash of Clans is getting an animated web series based on its Clash-A-Rama animated shorts.As opposed to the current shorts' 60 second run time, the new and improved Clash-A-Rama will be comprised of... | Read more »

Price Scanner via MacPrices.net

Never Settle for Low Performing Wifi With iOS...

AppYogi Software has announced the release of WiFi Signal Strength Status App 1.0, the company’s new utility developed exclusively for macOS. WiFi Signal Strength Status App features a unique, single... Read more

DietSensor, Inc., a developer of smart food and nutrition applications designed to fight diabetes and obesity and help improve overall fitness, has announced the launch of its DietSensor app for... Read more

Best Buy has dropped their price on the 64GB Apple TV to $159.99 including free shipping. That’s $40 off MSRP.
32GB Apple TVs are on sale right now for $98 on Sams Club’s online store. That’s $51 off... Read more

12-inch Retina MacBooks, Apple refurbished, n...

Apple has restocked a full line of Certified Refurbished 2016 12″ Retina MacBooks, now available for $200-$260 off MSRP. Refurbished 2015 models are available starting at $929. Apple will include a... Read more

Apple has Certified Refurbished 13″ MacBook Airs available starting at $849. An Apple one-year warranty is included with each MacBook, and shipping is free:
- 13″ 1.6GHz/8GB/128GB MacBook Air: $849 $... Read more

Apple refurbished iMacs available for up to $...

Apple has Certified Refurbished 2015 21″ & 27″ iMacs available for up to $350 off MSRP. Apple’s one-year warranty is standard, and shipping is free. The following models are available:
- 21″ 3.... Read more

MacTech is a registered trademark of Xplain Corporation. Xplain, "The journal of Apple technology", Apple Expo, Explain It, MacDev, MacDev-1, THINK Reference, NetProfessional, Apple Expo, MacTech Central, MacTech Domains, MacNews, MacForge, and the MacTutorMan are trademarks or service marks of Xplain Corporation. Sprocket is a registered trademark of eSprocket Corporation. Other trademarks and copyrights appearing in this printing or software remain the property of their respective holders. Not responsible for typographical errors.

All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.