Intel XENIX 286 Programmers Guide (86) - Tenox.tc

Intel XENIX 286 Programmers Guide (86) - Tenox.tc Intel XENIX 286 Programmers Guide (86) - Tenox.tc

09.06.2013 Views

XENIX Programming csh: C Shell Note: When setting $status (as in the cmp statement above), the status will change if anything is done between the cmp statement and the "if ($status != 0) then" statement. For example: cmp -s $i -/backup/$i :t # to set $status echo $status if ($status ! = 0) then The other control construct is a statement of the form if (expression) then command end if # if fi les are different, status = 1 # will echo 1 , but status will be set to 0 # now status = 0, but it should be 1 The placement of the keywords in this statement is not flexible due to the current implementation of the C shell. The following two formats are not acceptable to the C shell: and if (expression) # Won't work! then command end if if (expression) then command endif # Won't work The C shell does have another form of the if statement: if (expression) command which can be written if (expression) \ command Here we have escaped the newline for the sake of appearance. The command must not involve "1", "&", or ";" and must not be another control command. The second form requires the final backslash {\) to immediately precede the end-of-line. The more general if statements above also ad mit a sequence of else-if pairs followed by a single else and an endif, for example: if (expression) then commands else if (expression) then commands else end if commands 8-15

csh: C Shell XENIX Programming Another important mechanism used in C shell scripts is the colon (:) modifier. We can use the modifier :r here to extract the root of a file name. For example, this gives the variable i the value /mnt/foo.bar: % set i = /mnt/foo.bar If you give this command: % echo $i $i:r the result is /mnt/foo.bar /mnt/foo This example shows how the :r modifier strips off the trailing ".bar". Other modifiers take off the last component of a path name leaving the head :h or all but the last component of a path name leaving the tail :t. These modifiers are fully described in the entry csh in Appendix B, "Programming Commands." You can also use the command substitution mechanism to perform modifications on strings and then re-enter the C shell environment. Since each usage of this mechanism involves the creation of a new process, it is much more expensive to use than the colon (:) modification mechanism. Note also that the current implementation of the C shell limits the number of colon modifiers on a "$" substitution to 1. Thus produces % set i = /albic % echo $i $i:h:t !albic /a/b:t and does not do what you might expect. Finally, we note that the number sign (#) lexically introduces a C shell comment in C shell scripts (but not from the terminal). All subsequent characters on the input line after a number sign are discarded by the C shell. This character can be quoted using ' or \ to place it in an argument word. Using Other Control Structures The C shell also has control structures while and switch similar to those of C. These take the forms and 8-16 while ( expression ) commands end

<strong>XENIX</strong> Programming csh: C Shell<br />

Note: When setting $status (as in the cmp statement above), the status will change if<br />

anything is done between the cmp statement and the "if ($status != 0) then" statement.<br />

For example:<br />

cmp -s $i -/backup/$i :t # to set $status<br />

echo $status<br />

if ($status ! = 0) then<br />

The other control construct is a statement of the form<br />

if (expression) then<br />

command<br />

end if<br />

# if fi les are different, status = 1<br />

# will echo 1 , but status will be set to 0<br />

# now status = 0, but it should be 1<br />

The placement of the keywords in this statement is not flexible due to the current<br />

implementation of the C shell. The following two formats are not acceptable to the C<br />

shell:<br />

and<br />

if (expression) # Won't work!<br />

then<br />

command<br />

end if<br />

if (expression) then command endif # Won't work<br />

The C shell does have another form of the if statement:<br />

if (expression) command<br />

which can be written<br />

if (expression) \<br />

command<br />

Here we have escaped the newline for the sake of appearance. The command must not<br />

involve "1", "&", or ";" and must not be another control command. The second form<br />

requires the final backslash {\) to immediately precede the end-of-line.<br />

The more general if statements above also ad mit a sequence of else-if pairs followed by<br />

a single else and an endif, for example:<br />

if (expression) then<br />

commands<br />

else if (expression) then<br />

commands<br />

else<br />

end if<br />

commands<br />

8-15

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!