Understanding the shell's capabilities is the first step to writing good scripts. The shell is much more than a program launcher, although it does that very well. The shell is also called the "command interpreter," because it takes your typed command and does several things with it:
A shell can run several commands included in a single string, like the preceding example. This is a relatively simple command, but while executing it, the shell does several things:
Among the other things a shell can do (besides the "Cool Stuff That All Shells Have in Common" listed in Chapter 5, "Getting Started with SUSE Linux") are the following:
You will use all of these little wonders when writing your scripts in this chapter. Note The examples in this chapter will be for bash shell scripts, as it is by far the most popular (and the default) shell in SUSE Linux. You can write scripts to perform practically all the tasks included here on other shells as well. Consult the man page for your shell to check the proper syntax. You might also want to check out Sams Teach Yourself Shell Programming in 24 Hours by Sriranga Veeraraghavan or Linux Shell Scripting with Bash by Ken O. Burtch. Pattern-Matching in the ShellThe shell command line allows you to use strings of specially constructed patterns for wildcard matches. This is a different simpler capability that supported by GNU utilities such as grep, which can use more complex patterns, known as expressions, to search through files or directories or to filter data input to or out of commands. The shell's pattern strings can be simple or complex, but even using a small subset of the available characters in simple wildcards can yield constructive results at the command line. Some common characters used for shell pattern matching are:
Table 8.1 shows a few more common special characters. For a more comprehensive list, see the Advanced Bash Scripting Guide, listed in the References section.
Always be careful when using special characters. As you can see, some special characters have different meanings depending on their context, so your script may not behave if you don't use the character properly. Some characters can also be destructive, so make sure you use the right one. If you want to use a special character in a "normal" way, use the backslash (\) as an escape character in the script. To display the result of some calculation involving U.S. or Australian money, for example, include this string: echo \$(5153.27 + 654863.15) Note that mixing wildcards and regular expressions in shell scripts can lead to problems if you're not careful. For example, finding patterns in text is best left to regular expressions used with commands such as grep; simple wildcards should be used for filtering or matching filenames on the command line. Although both Linux command expressions and shell scripts can recognize the backslash as an escape character in patterns, the dollar sign ($) will have two wildly different meanings (single-character pattern matching in expressions and variable assignment in scripts). Redirecting Input and OutputYou can create, overwrite, and append data to files at the command line, using a process called input and output redirection. The shell recognizes several special characters for this process, such as >, <, or >>. In this example, the output of the ls command is redirected to create a file named textfiles.listing: ls *.txt >textfiles.listing Use output redirection with care because it is far too easy to overwrite existing files. All you need to do is redirect a different command to any existing file. The shell will perform the task you ask it to without checking for a file with the same name, or prompting you about your wishes concerning the soon-to-be-departed file. On the bright side, you can redirect output to add content to an existing file with the append operator, >>. So if you find something interesting to add to your textfiles collection, use two angle brackets instead of one in your command: ls /usr/share/doc/packages/emacs/doc/gnus-tut.txt >>textfiles.listing You can also turn your angle bracket around to take data and feed it to a command with input redirection, like this: cat < textfiles.listing Piping DataMany Linux commands can be run together in a single, connected command line to transform data from one form to another. Stringing commands together this way is known as using or creating pipes. Pipes take the output of one command and use it as input for the next command. This can be done nearly endlessly. Use the bar operator (|) to create pipes in the shell (it is used most often in this role, so many folks know the bar operator as the pipe already). Say you were the system administrator and needed to know who was currently logged in at a given moment. A piped command like this would give you a nice, tidy list: who | cut -ci-8 | sort -u | pr -l1 -8 -w78 -t You can see four commands at work here:
This is simple, and as your shell wizard skills improve, you will be able to do simply amazing things with a few keystrokes and some pipes. Background ProcessingOften in this book, you'll see a shell command with an ampersand (&) at the end of it. This is especially true when you use the shell to launch a graphical application. You may wonder what this is there for. The & takes advantage of the shell's ability to process commands in the background, while still running other programs from the same command line. Let's say you want to run the GKrellM system monitor from the shell. If you just type gkrellm at the prompt, the program will run, of course. But if you look at the shell, you'll see a dead cursor that cannot complete any more commands, at least until you exit out of GKrellM. If you then exit out of the monitor (right-click anywhere in the program and select Quit), your shell prompt returns, and you can type commands again. Using the up arrow to recall your last command from the command history, gkrellm again appears at the prompt. This time, add the background processing operator &, and press Enter. GKrellM runs as before, but bash now tells you the process number for this application, and then returns you to the prompt. You can now enter any other command you like. |