Basic Shell Control


Fedora includes a rich assortment of capable, flexible, and powerful shells. Each shell is different but has numerous built-in commands and configurable command-line prompts and might include features such as command-line history, the capability to recall and use a previous command line, and command-line editing. As an example, the bash shell is so powerful that it is possible to write a minimal web server entirely in bash's language using 114 lines of script (see the link at the end of this chapter).

Although there are many shells to choose from, most people stick with the default, bash. This is because bash does everything most people need to do, and more. Change your shell only if you really need to.

Table 15.1 lists each shell, along with its description and location, in your Fedora file system.

Table 15.1. Shells with Fedora

Name

Description

Location

ash

A small shell (sh-like)

/bin/ash

ash.static

A version of ash not dependent on software libraries

/bin/ash.static

bash

The Bourne Again SHell

/bin/bash

bsh

A symbolic link to ash

/bin/bsh

csh

The C shell, a symbolic link to tcsh

/bin/csh

ksh

The Korn shell

/bin/ksh,

  

/usr/bin/ksh

pdksh

A symbolic link to ksh

/usr/bin/pdksh

rsh

The restricted shell (for network operation)

/usr/bin/rsh

sash

A standalone shell

/sbin/sash

sh

A symbolic link to bash

/bin/sh

tcsh

A csh-compatible shell

/bin/tcsh

zsh

A compatible csh, ksh, and sh shell

/bin/zsh


Learning More about your Shell

All the shells listed in Table 15.1 have accompanying man pages, along with other documentation under the /usr/share/doc directory. Some of the documentation can be quite lengthy, but it is generally much better to have too much documentation than too little! The bash shell includes more than 100 pages in its manual, and the zsh shell documentation is so extensive that it includes the zshall meta man page (use man zshall to read this overview)!


The Shell Command Line

Having a basic understanding of the capabilities of the shell command line can help you write better shell scripts. If, after you have finished reading this short introduction, you want to learn more about the command line, check out Chapter 36, "Command Line Masterclass." You can use the shell command line to perform a number of different tasks, including

  • Searching files or directories with programs using pattern matching, or expressions; commands include the GNU gawk (linked as awk) and the grep family of commands, including egrep and fgrep

  • Getting data from and sending data to a file or command, known as input and output redirection

  • Feeding or filtering a program's output to another command (called using pipes)

A shell can also have built-in job-control commands to launch the command line as a background process, suspend a running program, selectively retrieve or kill running or suspended programs, and perform other types of process control.

Multiple commands can be run on a single command line using a semicolon to separate commands:

$ w ; free ; df   6:02pm  up 4 days, 24 min,  1 user,  load average: 0.00, 0.00, 0.00 USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU  WHAT bball    pts/0    shuttle.home.org  1:14pm  0.00s  0.57s  0.01s  w total       used       free     shared    buffers     cached Mem:        190684     184420       6264         76      17620     142820 -/+ buffers/cache:      23980      166704 Swap:      1277156       2516     1274640 Filesystem           1k-blocks       Used Available Use% Mounted on /dev/hda1             11788296    4478228   6711248  41% / none                     95340          0     95340   0% /dev/shm


This example displays the output of the w, free, and df commands. Long shell command lines can be extended inside shell scripts or at the command line by using the backslash character (\). For example,

$ echo ""this is a long \ > command line and"" ; echo ""shows that multiple commands \ > may be strung out."" this is a long command line and shows that multiple commands may be strung out.


The first three lines of this example are a single command line. In that single line are two instances of the echo command. Note that when you use the backslash as a line-continuation character, it must be the last character on the command line (or in your shell script, as you will see later in this chapter).

Using the basic features of the shell command line is easy, but mastering use of all features can be difficult. Entire books have been devoted to using shells, writing shell scripts, and using pattern-matching expressions. The following sections provide an overview of some features of the shell command line relating to writing scripts.

Grokking grep

If you plan to develop shell scripts to expand the capabilities of pattern-matching commands such as grep, you will benefit from learning more about using expressions. One of the definitive guides to using the pattern-matching capabilities of UNIX and Linux commands is Mastering Regular Expressions by Jeffrey E. F. Freidl (O'Reilly), ISBN: 0-596-00289-0.


Shell Pattern-Matching Support

The shell command line allows you to use strings of specially constructed character patterns for wildcard matches. This is a simpler capability than 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]*

  • \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\?

Note that the shell might not interpret some characters or regular expressions in the same manner as a Linux command, and mixing wildcards and regular expressions in shell scripts can lead to problems unless you're 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. And although both Linux command expressions and shell scripts can recognize the backslash as an escape character in patterns, the dollar sign ($) has two wildly different meanings (singlecharacter pattern matching in expressions and variable assignment in scripts).

Caution

Make sure that you read your command carefully when using wildcards; an all-too-common error is to type something like rm -rf * .txt with a space between the * and the .txt. By the time you wonder why the command is taking so long, bash will already have deleted most of your files. The problem is that it treats the * and the .txt separately. * matches everything, so bash deletes all your files.


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 possible to overwrite existing files. For example, specifying a different directory but using the same output filename overwrites the existing textfiles.listing:

$ ls /usr/share/doc/mutt-1.4/*.txt >textfiles.listing


Fortunately, most shells are smart enough to recognize when you might do something foolish. Here, the bash shell warns that the command is attempting to redirect output to a directory:

$ mkdir foo $ ls >foo bash: foo: Is a directory


Output can be appended to a file without overwriting existing content by using the append operator, >>. In> append operator> this example, the directory listing is appended to the end of textfiles.listing instead of overwriting its contents:

$ ls /usr/share/doc/mutt-1.4/*.txt >>textfiles.listing


You can use input redirection to feed data into a command by using the < like this:

$ cat < textfiles.listing


You can use the shell here operator, <<, to specify the end of input on the shell command line:

$ cat >simple_script <<DONE > echo ""this is a simple script"" > DONE $ cat simple_script echo ""this is a simple script""


In this example, the shell feeds the cat command you are typing (input) until the pattern DONE is recognized. The output file simple_script is then saved and its contents verified. This same technique can be used in scripts to create content based on the output of various commands and define an end-of-input or delimiter.

Piping Data

Many Linux commands can be used in concert in a single, connected command line to transform data from one form to another. Stringing Linux commands together in this fashion is known as using or creating pipes. Pipes are created on the command line with the bar operator (|). For example, a pipe can be used to perform a complex task from a single command line like this:

 $ find /d2 -name '*.txt' -print | xargs cat | \ tr ' ' '\n' |  sort | uniq >output.txt


This example takes the output of the find command to feed the cat command (via xargs) the name all text files under the /d2 command. The content of all matching files is then fed through the tr command to change each space in the data stream into a carriage return. The stream of words is then sorted, and identical adjacent lines are removed using the uniq command. The output, a raw list of words, is then saved in the file named output.txt.

Background Processing

The shell allows you to start a command and then launch it into the background as a process by using an ampersand (&) at the end of a command line. This technique is often used at the command line of an X terminal window to start a client and return to the command line. For example, to launch another terminal window using the xterm client,

$ xterm & [3] 1437


The numbers echoed back show a number (3 in this example), which is a job number, or reference number for a shell process, and a Process ID number, or PID (1437 in this example). The xterm window session can be killed by using the shell's built-in kill command, along with the job number like this:

$ kill %3


Or the process can be killed by using the kill command, along with the PID, like so:

$ kill 1437


Background processing can be used in shell scripts to start commands that take a long time, such as backups:

# tar -czf /backup/home.tgz /home &




Red Hat Fedora 5 Unleashed
Red Hat Fedora 5 Unleashed
ISBN: 067232847X
EAN: 2147483647
Year: 2004
Pages: 362

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