Originally published by Robert Beisert at fortcollinsprogram.robert-beisert.com

Linux + C – Scripting in the Shell

We’ve used the terminal a number of times. It turns out that we can write simple scripts to automate some of our routines.

The Shell

A shell is a specialized interpreter which has access to all the computer’s resources and programs. Perhaps the most ubiquitous UNIX shell is the Bourne Shell (/bin/sh).

Stephen Bourne’s shell was written at Bell Labs, and included in the first UNIX distributions. Kernighan and Pike used this shell in their work, The UNIX Programming Environment, one of the classic texts of C and UNIX/Linux programming.

There are other popular shells, notably the C Shell (csh or tcsh), but we’ll be focusing on sh for this tutorial.

Variables

It can be a pain to retype values over time. The Bourne Shell provides environment variables which can store strings, integers, and paths for our use.

When we fill a variable, we use the simple declaration format:

VARIABLE=VALUE

Note: There are no spaces between these fields. If you want the value to have spaces, you have to surround it with ‘ ‘ marks.

For example, if we want all of our operations to follow a particular path, we can simply declare it as such:

PATH=/home/bob/Documents/Folder/…

From that point on, we can reference our path with the PATH environment variable. If we want to run a program at the path location (say, ./panda), we can type the following:

$PATH/panda

Note: The $ means “the value inside of this variable”.

Running Commands

A Shell script basically looks like a series of terminal commands. For example, we can run any of the programs in our Programming directory by typing the value into our script.

./input

./do_math

./loop

./pipe_filter

./args 5 5

We can also run commands within other commands using the ` symbol (that one between the ESC key and the TAB key). For example, if we want to print a string with today’s date, we could write:

echo “Today’s date is: `date`”

This can allow us to do very interesting things without pipes and redirection.

Wildcards

There are some useful characters that allow us to perform a lot of work very quickly. For example, we have the *, which the Shell will automatically fill in. If you want to open all of your files in vi (which I don’t recommend as such), you can simply type the following, and the shell will figure it out:

vi *

The * wildcard can be used to fill in any number of characters in any location. For example, if we want to open all of our C Headers in vi, we could type the following:

vi *.h

Or, if we wanted to fill in a portion of a file name, we could use the wildcard as such:

vi panda_*.c

We have a few other wildcards of interest:

  • ? – Like *, but represents EXACTLY ONE character
  • [abc-g] – like ?, but only works for the letters a, b, or anything in the range of c-g
  • \ – The escape character, which removes the Shell meaning of a character (for example, \$ would act like a character $, not like the shell $)

Why?

It’s a valid question. Why would we write a shell script, when we could just type all these things out ourselves?

We could ask the same question about makefiles.

The fact is, the time you spend retyping everything could be better served doing…basically anything else. Programmers are the hardest working lazy people on this planet. Any way we can make our lives 5% easier, we do it.

photo by: