Linux: Notes on Shell Scripting - dummies

By Emmett Dulaney

Linux gives you many small and specialized commands, such as bash (short for Bourne-Again Shell) — the default shell in most Linux systems. You can learn how to write simple shell scripts, which are used to automate various tasks.

Simple bash routines that allow you to run commands and repeat operations a number of times introduce you to some of the power available through shell scripting.

This article builds upon that knowledge by showing how to incorporate two powerful tools — sed and awk — into your scripts. These two utilities move your scripts to the place where the only limit to what you can do becomes your ability to figure out how to ask for the output you need.

Although sed is the stream editor and awk is a quick programming language, they complement each other so well that it’s not uncommon to use one with the other. The best way to show how these tools work is to walk through some examples.

As with any other aspect of computing, it takes a while to get used to shell scripting. After you become comfortable writing scripts, however, you’ll find that you can automate any number of operations and simplify your task as an administrator. The following tips can be helpful to keep in mind:

  • After you create a script, you can run it automatically on a one-time basis by using at, or on a regular basis by using cron.

  • You can use conditional expressions, such as if, while, and until, to look for events to occur (such as certain users accessing a file they should not) or to let you know when something that should be there goes away (for example, a file is removed or a user terminates).

  • You can set permissions on shell scripts in the same way you set permissions for other files. For example, you can create scripts that are shared by all members of your administrative group (use case to create menus based upon LOGNAME).