To access the contents, click the chapter and section titles.
Complete Idiot's Guide to Linux
(Publisher: Macmillan Computer Publishing)
Author(s): Manuel Ricart
ISBN: 078971826x
Publication Date: 12/22/98
Chapter 14 Putting the Shell to Work
In This Chapter
- Output and Input Redirection
- Chaining Commands Together
- Regular Expressions
- Command History
In Chapters 1113, you learned a few basics about commands and shells. Although single commands will probably be most of what you do, the real power of shell comes when you are able to string a series of commands together to perform more complex tasks than any single command can accomplish. Shells allow you to do so, and in this chapter youll learn how to take advantage of this ability, along with a few other nifty command shell tricks.
Using More Than One Command at a Time
You can issue several commands at the same time; just separate each command with a semicolon (;). Heres an example:
[alberto@digital alberto]$ pwd; cd ..; pwd; ls -lg
/home/alberto
/home
total 3
drwxr-xr-x 14 alberto users 1024 Aug 18 19:01 alberto/
drwxr-xr-x 5 root root 1024 Aug 14 08:09 ftp/
drwxr-xr-x 7 root root 1024 Aug 14 08:09 httpd/
[alberto@digital home]$
In this example I did a number of things:
I listed the current working directory with pwd, and then I changed directories to the parent (up one) directory by typing two periods (..). I also listed the new current working directory and asked for a long listing of the files there.
When I asked to cd up a directory, it did, and when all commands finished executing, I was left where the last command to affect my current directory stopped (the /home directoryyou can tell this if you look at the end of my shell prompt).
Command Groups and Subshells
Command groups allow you to execute a series of commands in a subshell. When you create a command group, Linux fires up a new shell subprocess different from your current shell to execute it. When the command group finishes executing, it returns control to your shell.
You create a command group by enclosing the commands in parentheses, like this:
[alberto@digital alberto]$ (pwd; cd ..; pwd; ls -lg)
/home/alberto
/home
total 3
drwxr-xr-x 14 alberto users 1024 Aug 18 19:01 alberto/
drwxr-xr-x 5 root root 1024 Aug 14 08:09 ftp/
drwxr-xr-x 7 root root 1024 Aug 14 08:09 httpd/
[alberto@digital alberto]$
If you are really paying attention, you will also notice that when you execute a command group, the current environment is not affected. Note how my starting directory is alberto, and my final directory after the command group ends remained at alberto
yet one of the commands moved up a directory!
The reason for this move is that execution of command groups creates a subshell. Subshells have their own environment. (Their initial environment matches your current environment.) When the subshell executed the cd command, it changed the environment of the subshell. However, it did not affect the environment of the parent shell, the shell on which I typed the command.
Redirection
Much of the usefulness of shells comes from the capability to redirect input and output to and from files. Most shell programs are designed to process data using streams.
A stream is nothing more than data flowing as input or output. For example, a commands input stream usually comes from your keyboard, and its output stream usually goes to your terminals display. Under UNIX all programs communicate with the oustide world using three streams:
- STDOUTThe standard output stream (usually the display)
- STDINThe standard input stream (usually the keyboard)
- STDERRThe standard error stream (usually the display
Streams can be interconnected easily or wired in different ways through a shell. Your shell can cause a program to use a file for its input stream and to put results back into a file using the output stream. This is called redirection.
|