Code 128 10 Programming the Bourne Again Shell in Objective-C Encoder barcode code 128 in Objective-C 10 Programming the Bourne Again Shell

1010. use none none writer todraw none in printing code 128 The first part of this ch none for none apter covers programming control structures, also called 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 a shell. Visual Studio 2008 398 10 Programming the Bourne Again Shell script to accept user inp none for none ut. The section on the exec builtin demonstrates how to use exec to execute a command efficiently by replacing a process and explains how to use exec 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 the signal 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 10-6 on page 459 lists some of the more commonly used builtins. Next the chapter examines arithmetic and logical expressions as well as 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..

Do not name a shell scrip t test tip You can unwittingly create a problem if you give a shell script the name test because a Linux utility. has the same name. Depend none none ing on how the PATH variable is set up and how you call the program, you may run either 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.

Return to them when you feel comfortable with the basic concepts.. Control Structures The control flow commands alter the order of execution of commands within a shell script. The TC Shell uses a different syntax for these commands than the Bourne Again Shell does; see page 378. Control structures include the if.

..then, for., while, until, and case statements.

In addition, the break and continue statements work in conjunction with the control structures to alter the order of execution of commands within a script.. if...then The if...then control structure has the following syntax: if test-command then commands fi Control Structures if test-command False True then commands Figure 10-1. An if...then flowchart The bold words in the syn tax description are the items you supply to cause the structure to have the desired effect. The nonbold words are the keywords the shell uses to identify the control structure..

test builtin Figure 10-1 shows that th e if statement tests the status returned by the test-command and transfers control based on this status. The end of the if structure is marked by a fi statement (if spelled backward). The following script prompts for two words, reads them, and then uses an if structure to execute commands based on the result returned by the test builtin (tcsh uses the test utility) when it compares the two words.

(See page 854 for information on the test utility, which is similar to the test builtin.) The test builtin returns a status of true if the two words are the same and false if they are not. Double quotation marks around $word1 and $word2 make sure test works properly if you enter a string that contains a SPACE or other special character:.

$ cat if1 echo -n "word 1 none for none : " read word1 echo -n "word 2: " read word2 if test "$word1" = "$word2" then echo "Match" fi echo "End of program.".
Copyright © . All rights reserved.