Shell Scripting vs Programming – Introduction

Spread the love

It’s time for a new series focusing on the Linux shell. In my last series, I taught you how to work in the shell to manipulate files and directories, execute commands, find things, and work with a powerful editor. This time around, I’m going to talk about programming in the shell. What I want to do is get you comfortable with the idea of writing small, useful scripts to do your job as an administrator of your Linux domain, no matter how small or large. To that end, I will concentrate on bash. Then, I’ll cover some of the other scripting languages out there in order to see how they might best help you do your job. And so today is just an introduction. I’ll pick up the pace and start regular tutorials in the next couple of days. When I wrap-up today’s introduction, I’ll leave you with a teaser to show you just how easy it is to write a shell script.

One of my favorite lines is “I am not a programmer,” which isn’t to say that I don’t know how to program or that I haven’t wrestled with a number of different programming languages over the years. I suppose that sounds a bit like Dr. McCoy saying, “I’m a doctor, not a fill-in-the-blank.” Names like COBOL, FORTRAN, BASIC, C, C++, Ada, Perl, and the dreaded assembler all come to mind as a reminder. I have been known to write programs and I am likely to do so again. It just doesn’t happen to be the sort of thing that excites me. Systems administration excites me. So does network design, security, systems integration, and the great bugaboo, technical support (I always loved puzzles).

I’m like the chef who likes to put a number of ingredients together to create a new culinary masterpiece. Like that chef, I rely on others to provide me with the ingredients to do my work. Consequently, I have a deep admiration for professional (and amateur) programmers who can take a complex project and devote weeks or even months to bring out the tools that people like me rely on. If I haven’t publicly thanked you lately, “Thanks to you all.”

That said, even systems administrators need to program on some level. A single tool is rarely the answer to a single problem, and sometimes, only by assembling the right blend of tools will you provide exactly the solution you seek. Putting those tools together requires programming. The average Linux distribution comes with a plethora of programming and program development environments. Luckily, the average administrator can satisfy all his or her programming needs with a couple of very neat languages.
One of those languages is something you are already using.

Shells As Far As the Eye Can See

The shell is the most basic of environments for working with your Linux system. Whatever you may think of working in a text environment, I guarantee that once you have fully experienced the power of simple text, you will be forever convinced. Text is compact. Text is fast. System administration over a network is best experienced at the shell level. Those forced to resort to graphical tools over a slow Internet connection are also quickly converted.

What many people do not realize is that the shell is actually a programming language. There are many shells. The most common in the Linux world is called bash, the Bourne Again Shell. Other shells you may find on your Linux system include sh, the classic Bourne shell (I have yet to find a major Linux or UNIX distribution that did not include it), pdksh (the public domain Korn shell), and that old programmer’s favorite, csh (the C shell).

The list of shells I’ve given you so far is but a small subset. There’s ash (stands for “a shell”), tcsh (an enhanced C shell), and zsh (the Z shell). Like walking along the beach and building a collection of interesting and unique shell specimens, a little tour through your favorite software repository, such as Freecode, will show you that people are constantly playing with and designing shells. Far from being an exhaustive list of the shells that exist for Linux, my list is merely a collection of the more popular ones. Certainly, all of these are likely to be on your distribution CD. Covering all these shells in detail would demand that I write a book on the subject, or perhaps several books, so I won’t even pretend to attempt it.

Let’s write your first shell script.

It’s Commands All The Way Down

A shell script can be just a collection of commands saved in a file. Too easy? Say that throughout the day, I regularly check the time and date, print the disk-free information, and check how long my system has been running with uptime. I can open a blank file with my favorite editor and just type each command on its own line.

date
df
uptime

Since I’m not covering editors in this teaser, let me show you how to write a script without one. Start by typing the following in your favorite terminal program (e.g. gnome-terminal, konsole, etc) :

cat > check3

Now type those three commands, one by one, pressing the return (or Enter) key after each one.  What you are doing here is writing to a file called check3, a completely arbitrary name for our script. After you enter the last command, hold down the Ctrl key and press “d” (i.e. Ctrl+d). That will terminate the file creation and save our script. You can then can execute this script by typing the following:

bash check3

That’s it. The shell, bash, executes the three commands in that file and exits.

And this is where I leave it for today.

As usual, if you wish to comment, please do so on Google Plus in the comment form below, or over here on Facebook and add me to your circles or friend list if you haven’t already done so; oh yeah, if you’re on Twitter,follow me there. Also, make sure you sign up for the mailing list over here so that you’re always on top of what you want to be on top of.  And a final request, please share this article with your favorite Linux and FOSS group or news site; I would sincerely appreciate it. Until next time . . .

A votre santé! Bon appétit!

Liked it? Take a second to support Cooking With Linux on Patreon!
Become a patron at Patreon!