18 – Shell Programming
December 5, 2009 – 10:16 pmPlease note… This information no longer exists at the referenced locations. This is only a copy of what was available in 2003.
Basic Linux Training™
Shell Programming
Henry White
Table of Contents
Shell Programming
Go easy on this stuff
You can quickly and easy reach ‘information overload’ if you rush into this! Nothing here is especially difficult, but almost all of it will be completely new to most of you, and like all things UNIX/Linux, the learning curve is pretty steep.
The various Unix shells can be used for programming as well as simply interpreting commands. This is similar to writing DOS batch files – both are command interpreters with built-in commands and programming language. We’ve already covered the basic shell functions in the previous lesson; now we turn our attention to shell scripts.
Although most people are familiar with the ability of the shell to interpret commands as they are typed in at the terminal, many are not familiar with its ability to execute programs or shell scripts. The tools that can be used to create a shell script program are all built into the shell (whichever shell you are using will have it’s own set of built-in commands, and their syntax may vary slightly from one shell to another). Most of these built-in commands are rarely used at the command line (except by hard-core geeks
) although they could be. But that’s really the whole point – using shell scripts for complicated and/or repetitive tasks. Thus a function may be defined to execute a series of commands (separating the commands with a semi-colon (;) or a new line).
The most common mistake is not ensuring that any shell script you create will run using by starting the shell script with:
#! /bin/sh
(or pointing to whichever shell you are using).
A couple of other common mistakes that make it difficult to get help from others are not setting the correct permissions, or having messed around with the path and having a conflict that may be hard for someone else to track down.
Obviously, a safe step to take is to check if the function name that you intend to use is NOT currently in use anywhere in the path. Names like test may immediately pop into mind but are not always practical; in this particular instance the name test has already been taken on Linux systems; mytest would be acceptable.
Within shell scripts, functions are used to repeat the same logic over and over. This makes the script more readable because the logic of a long shell script will be easier to follow.
Just a suggestion: at the earliest stage, you’re better off if you can just read shell scripts. If you understand what they are intended to do, then you should move on to writing your own. You can’t possibly learn everything in the textbook in a few weeks; so plan to come back to these more technical areas and continue working on them longer after the course is over. Like batch files in DOS, shell scripts may be as much as you’ll ever want to do, or it may be your introduction to programming.
Assignments
Terms and Concepts:
Define and add these to your glossary:
Utilities & Commands
- break
- continue
- exec
- fi
- for…in
- for
- if…then…elif
- if…then…else
- if…then
- trap
- until
- while
Terms & Concepts
- case
- control structure
Online:
- http://www.shelldorado.com/ – Steven Heiner’s Shelldorado
- http://home.earthlink.net/~selk/shell_help.html – A tutorial on how to write shell scripts
Copyright © 1997-2003 Henry White. All Rights Reserved.
Reproduction or redistribution without prior written consent is strictly prohibited. Address comments and inquiries to info@basiclinux.net
Sorry, comments for this entry are closed at this time.