The Power of the Command Line


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:

1.

First, it determines whether the command is something built into it. If it is, it runs the command.

2.

If not, it checks the search path environment variable to find the command (unless you've located the command yourself by typing in the full path).

3.

When it locates the command you've asked for, the shell makes a fork call on the Linux kernel to copy the program (so it will return to the shell prompt after the program completes its task). The program then makes another exec call on the kernel to execute the command.

4.

Finally, the shell uses some rules to determine what a command like cat *.txt *.sxw > doclist is trying to accomplish (this is where the interpretation comes in) and delivers a result.

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:

  • Launches a program It runs cat, a program that displays files on the screen.

  • Does job control and gets out of the way The shell pretends it isn't there while Cat searches for the text of all files in the current directory with .txt and .sxw extensions and then displays it.

  • Redirects output Before the text display hits the screen, the shell leaps back into action and intercepts the system call, putting the display (that is, the text of all files with those extensions) into a file called doclist and saving it to the same directory.

Running Shell Commands in Konqueror

Do you want to combine the power of the shell with the relative convenience of a GUI file manager? The KDE file manager, Konqueror, lets you run any shell command from its interface.

Go to Tools, Execute Shell Command (or press Ctrl+E). If you have a file or directory selected, you will see its name in the edit box, but running a command on that file isn't necessary. Type any command (or series of commands) and it will run; any messages pop up in a separate window.

If you have opened Konqueror as the SuperUser, you will have all the privileges of rank when you run commands, but you cannot log in as SuperUser (or run any command that requires extra input) from a plain Konqueror window.


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:

  • Search files and directories for patterns or regular expressions using locate, gawk, or grep.

  • Feed the results or output of one program into another program for further processing.

  • Run multiple commands separated by a semicolon.

  • Create five empty files with a single command:

    touch file {a,b,c,d,e}

  • Run a program in the background.

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 Shell

The 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:

  • * Matches any character. For example, to find all files in the current directory ending in .txt, you could use

    ls *.txt

  • ? Matches a single character. For example, to find all files in the current directory ending in the extension d?c (where ? could be 0-9, a-z, or A-Z):

    ls *.d?c

  • [xxx] or [x-x] Matches a range of characters. For example, to list all files in a directory with names containing numbers:

    ls [0-9]*

    To find only those with characters 1, 5, or 8, this would be

    ls [158]*

  • \x Matches or escapes a character such as ? or a tab character. For example, to create a file with a name containing a question mark:

    touch foo\?

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.

Table 8.1. Special Characters in bash

Character

Meaning

#

Beginning of a comment line (except when it's part of a #!).

$

Shell variable name.

;

Command separator.

.

Current directory.

..

Parent directory.

~

(tilde) Home directory (this character is located to the left of the number 1 on your keyboard; hold down the shift key to use it).

`

(backtick) Command substitution; tells the shell to use the results of the upcoming command as part of the next task.

>

Redirects the output of a script to a file. If the file exists, it will be overwritten.

>>

Appends the output of a script to an existing file. If the file does not exist, it will be created.

|

Pipe. Helps to chain commands and scripts together, passes the output of one command as an argument in the next.

&

Run this in the background; it allows you to return to the shell prompt.


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 Output

You 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 Data

Many 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:

1.

who tells you the users who are logged in. This result is processed by

2.

cut, which outputs only the usernames to the screen (instead of the whole line), but first

3.

sort puts the names in alphabetical order, and removes from the list those users who are logged in more than once. Finally,

4.

pr takes the list, one per line, and delivers it to the screen in eight columns.

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 Processing

Often 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.



SUSE Linux 10 Unleashed
SUSE Linux 10.0 Unleashed
ISBN: 0672327260
EAN: 2147483647
Year: 2003
Pages: 332

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net