8.9. Control StructuresThe C shell supports a wide range of control structures that make it suitable as a high-level programming tool. Shell programs are usually stored in scripts and are commonly used to automate maintenance and installation tasks. Several of the control structures require several lines to be entered. If such a control structure is entered from the keyboard, the shell prompts you with a ? for each additional line until the control structure is ended, at which point it executes. There now follows a description of each control structure, in alphabetical order. I made the C shell examples correspond closely to the Bash examples so that you can compare and contrast the two shells. 8.9.1. foreach .. endThe foreach command allows a list of commands to be repeatedly executed, each time using a different value for a named variable. Figure 8-22 gives the syntax.
Here's an example of a script that uses a foreach control structure: % cat foreach.csh ...list the script. # foreach color (red yellow green blue) # four colors echo one color is $color end % tcsh foreach.csh ...execute the script. one color is red one color is yellow one color is green one color is blue % _\ 8.9.2. gotoThe goto command allows you to jump unconditionally to a named label. To declare a label, simply start a line with the name of the label followed immediately by a colon. Figure 8-23 gives the syntax of a goto command.
Use goto sparingly to avoid nasty spaghettilike code (even if you like spaghetti). Here's an example of a simple goto: % cat goto.csh ...list the script. # 8.9.3. if .. then .. else .. endifThere are two forms of if command. Figure 8-24 gives the syntax of the first form, which supports a simple one-way branch.
Here is an example of this form of if: % if (5 > 3) echo five is greater than 3 five is greater than three % _ The second form of the if command (Figure 8-25) supports alternative branching.
Here's an example of the second form of if: % cat if.csh ...list the script. # echo -n 'enter a number: ' # prompt user. set number = $< # read a line of input. if ($number < 0) then echo negative else if ($number == 0) then echo zero else echo positive endif % tcsh if.csh ...execute the script. enter a number: -1 negative % _ 8.9.4. onintrThe onintr command (Figure 8-26) allows you to specify a label that should be jumped to when the shell receives a SIGINT signal. This signal is typically generated by a Control-C from the keyboard, and is described in more detail in Chapter 12, "Systems Programming."
Here's an example: % cat onintr.csh ...list the script. # onintr controlC # set Control-C trap. while (1) echo infinite loop sleep 2 end controlC: echo control C detected % tcsh onintr.csh ...execute the script. infinite loop infinite loop ^C ...press Control-C. control C detected % _ 8.9.5. repeatThe repeat command allows you to execute a single command a specified number of times. Figure 8-27 gives the syntax.
Here is an example of the use of repeat: % repeat 2 echo hi there ...display two lines. hi there hi there % _ 8.9.6. switch .. case .. endswThe switch command supports multiway branching based on the value of a single expression. Figure 8-28 shows the general form of a switch construct.
Here's an example of a script called "menu.csh" that makes use of a switch control structure: # echo menu test program set stop = 0 # reset loop termination flag while ($stop == 0) # loop until done cat << ENDOFMENU # display menu 1 : print the date. 2, 3: print the current working directory 4 : exit ENDOFMENU echo echo -n 'your choice? ' # prompt set reply = $< # read response echo "" switch ($reply) # process response case "1": date # display date breaksw case "2": case "3": pwd # display working directory breaksw case "4": set stop = 1 # set loop termination flag breaksw default: # default echo illegal choice # error breaksw endsw echo end Here's the output from the "menu.csh" script: % tcsh menu.csh menu test program 1 : print the date. 2, 3: print the current working directory 4 : exit your choice? 1 Sat May 14 00:50:26 CST 2005 8.9.7. while .. endThe built-in while command allows a list of commands to be repeatedly executed as long as a specified expression evaluates to true (nonzero). Figure 8-29 gives the syntax.
Here's an example of a script that uses a while control structure to generate a small multiplication table: % cat multi.csh ...list the script. # set x = 1 # set outer loop value while ($x <= $1) # outer loop set y = 1 # set inner loop value while ($y <= $1) # inner loop @ v = $x * $y # calculate entry echo -n $v " " # display entry (tab in quotes) @ y ++ # update inner loop counter end echo "" # newline @ x ++ # update outer loop counter end % tcsh multi.csh 7 ...execute the script. 1 2 3 4 5 6 7 2 4 6 8 10 12 14 3 6 9 12 15 18 21 4 8 12 16 20 24 28 5 10 15 20 25 30 35 6 12 18 24 30 36 42 7 14 21 28 35 42 49 % _ |