Chapter 11. Programming The Bourne Again Shell

 < Day Day Up > 

IN THIS CHAPTER

Control Structures 436

File Descriptors 470

Parameters and Variables 474

Array Variables 474

Locality of Variables 475

Special Parameters 478

Positional Parameters 480

Builtin Commands 487

Expressions 501

Shell Programs 510

A Recursive Shell Script 510

The quiz Shell Script 513

Chapter 5 introduced the shells and Chapter 8 went into detail about the Bourne Again Shell. This chapter introduces additional Bourne Again Shell commands, builtins, and concepts that carry shell programming to a point where it can be useful. The first part of this chapter covers programming control structures, which are also known as control flow constructs. These structures allow you to write scripts that can loop over command line arguments, make decisions based on the value of a variable, set up menus, and more. The Bourne Again Shell uses the same constructs found in such high-level programming languages as C.

The next part of this chapter discusses parameters and variables, going into detail about array variables, local versus global variables, special parameters, and positional parameters. The exploration of builtin commands covers type, which displays information about a command, and read, which allows you to accept user input in a shell script. The section on the exec builtin demonstrates how exec provides an efficient way to execute a command by replacing a process and explains how you can use it to redirect input and output from within a script. The next section covers the TRap builtin, which provides a way to detect and respond to operating system signals (such as that which is generated when you press CONTROL-C). The discussion of builtins concludes with a discussion of kill, which can abort a process, and getopts, which makes it easy to parse options for a shell script. (Table 11-6 on page 500 lists some of the more commonly used builtins.)

Table 11-6. bash builtins

Builtin

Function

:

Returns 0 or true (the null builtin; page 495)

. (dot)

Executes a shell script as part of the current process (page 259)

bg

Puts a suspended job in the background (page 273)

break

Exits from a looping control structure (page 459)

cd

Changes to another working directory (page 82)

continue

Starts with the next iteration of a looping control structure (page 459)

echo

Displays its arguments (page 53)

eval

Scans and evaluates the command line (page 318)

exec

Executes a shell script or program in place of the current process (page 491)

exit

Exits from the current shell (usually the same as CONTROL-D from an interactive shell; page 480)

export

Places the value of a variable in the calling environment (makes it global; page 475)

fg

Brings a job from the background into the foreground (page 272)

getopts

Parses arguments to a shell script (page 497)

jobs

Displays list of background jobs (page 271)

kill

Sends a signal to a process or job (page 693)

pwd

Displays the name of the working directory (page 81)

read

Reads a line from standard input (page 487)

readonly

Declares a variable to be readonly (page 281)

set

Sets shell flags or command line argument variables; with no argument, lists all variables (pages 319, 356, and 484)

shift

Promotes each command line argument (page 483)

test

Compares arguments (pages 437 and 794)

times

Displays total times for the current shell and its children

trap

Traps a signal (page 493)

type

Displays how each argument would be interpreted as a command (page 487)

umask

Returns the value of the file-creation mask (page 810)

unset

Removes a variable or function (page 281)

wait

Waits for a background process to terminate (page 381)


Next the chapter examines arithmetic and logical expressions and the operators that work with them. The final section walks through the design and implementation of two major shell scripts.

This chapter contains many examples of shell programs. Although they illustrate certain concepts, most use information from earlier examples as well. This overlap not only reinforces your overall knowledge of shell programming but also demonstrates how you can combine commands to solve complex tasks. Running, modifying, and experimenting with the examples in this book is a good way to become comfortable with the underlying concepts.

tip: Do not name a shell script test

You can unwittingly create a problem if you give a shell script the name test because a Linux utility has the same name. Depending on how the PATH variable is set up and how you call the program, you may run your script or the utility, leading to confusing results.


This chapter illustrates concepts with simple examples, which are followed by more complex ones in sections marked "Optional". The more complex scripts illustrate traditional shell programming practices and introduce some Linux utilities often used in scripts. You can skip these sections without loss of continuity the first time you read the chapter. Return to them later when you feel comfortable with the basic concepts.

     < Day Day Up > 


    A Practical Guide to LinuxR Commands, Editors, and Shell Programming
    A Practical Guide to LinuxR Commands, Editors, and Shell Programming
    ISBN: 131478230
    EAN: N/A
    Year: 2005
    Pages: 213

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