📄 unx13.htm
字号:
% ps -e</PRE>
<P>except that in the second case the results of each command would appear between the command input lines.
<BR></P>
<P>When the semicolon is used to separate commands on a line, the commands are executed in sequence. The shell waits until one command is complete before executing the next. You can also execute commands simultaneously (see the section titled
"Executing Commands in the Background") or execute them conditionally, which means that the shell executes the next command only if the first command succeeds or fails (see the section titled "Executing Commands Conditionally").
<BR></P>
<H5 ALIGN="CENTER">
<CENTER><A ID="I12" NAME="I12">
<FONT SIZE=3><B>Entering Commands Too Long for One Line</B>
<BR></FONT></A></CENTER></H5>
<P>Sometimes command lines get quite lengthy. On some terminals, when you reach the edge of the display screen the input autowraps to the next line, but depending on terminal settings, some do not. It would be nice if you could type part of a command on
one line and enter the remainder of the command on a second line. This can be accomplished by escaping the newline character.
<BR></P>
<P>Remember that the shell sees a line of input as a statement terminated with a newline character. But the newline character is also considered to be a white space character. If you end a line with a backslash (\), the next character—the newline
character—will be treated literally, meaning that the shell will not interpret the newline character as the end of the line of input.
<BR></P>
<PRE>% echo Now is the time for all good men \_
to come to the aid of the party.
Now is the time for all good men to come to the aid of the party.</PRE>
<H5 ALIGN="CENTER">
<CENTER><A ID="I13" NAME="I13">
<FONT SIZE=3><B>Executing Commands in the Background</B>
<BR></FONT></A></CENTER></H5>
<P>Normally when you execute commands, they are executed in the foreground. This means that the command has the system's undivided attention, and you can't do anything else until the command finishes executing. For commands that take a long time to
execute, however, this can be a problem. To free your system without waiting for the command to finish, you can execute the command in the background by putting an ampersand (&) at the end of the command:
<BR></P>
<PRE>% who -H &
[1] + Running who -H &
%</PRE>
<P>You also can run multiple commands in the background simultaneously:
<BR></P>
<PRE>% who -H & df -v & ps -e &</PRE>
<P>A command executing in the background is referred to as a job, and each job is assigned a job number—the bracketed number in the preceding example. C shell provides you with several commands for managing background jobs; see the section later in
this chapter titled "Job Control."
<BR></P>
<H5 ALIGN="CENTER">
<CENTER><A ID="I14" NAME="I14">
<FONT SIZE=3><B>Repetitively Executing a Command—</B><B><I>repeat</I></B>
<BR></FONT></A></CENTER></H5>
<P>You can use the repeat command to execute some other command a specified number of times. While the repeat command doesn't see frequent use, it can on occasion be quite handy. For example, if you had stored some text in a model file, and wanted to make
five copies of it, you could do so easily with the command
<BR></P>
<PRE>repeat 5 cat model.txt new.txt</PRE>
<P>Or, if you were writing a shell script to print a document, you might use the command
<BR></P>
<PRE>repeat 5 echo *******************************</PRE>
<P>to mark its first page clearly as the start of the document.
<BR></P>
<P>The syntax of the repeat command is as follows:
<BR></P>
<PRE>repeat count command</PRE>
<P>For count, specify a decimal integer number. A count of zero is valid and suppresses execution of the command.
<BR></P>
<P>For command, specify a simple command that is subject to the same restrictions as the first format of the if statement. The command is scanned for variable, command, and history substitutions, filename patterns, and quoting. It cannot be a compound
command, a pipeline, a statement group (using {}), or a parenthesized command list.
<BR></P>
<P>Any I/O redirections are performed only once regardless of the value of count. For example, repeat 10 echo Hello >hello.list would result in ten lines of Hello in a file named hello.list.
<BR></P>
<H5 ALIGN="CENTER">
<CENTER><A ID="I15" NAME="I15">
<FONT SIZE=3><B>Executing Commands in a Subshell—</B><B><I>()</I></B>
<BR></FONT></A></CENTER></H5>
<P>A command (or a list of commands separated with semicolons) enclosed in parentheses groups the command or commands for execution in a subshell. A subshell is a secondary invocation of the shell, so any change to shell variables, the current directory,
or other such process information lasts only while executing the commands in the group. This is a handy way, for example, to switch to another directory, execute a command or two, and then switch back without having to restore your current directory:
<BR></P>
<PRE>% (cd /home/bill; cp *.txt /home/john)</PRE>
<P>Without the parentheses, you would have to write:
<BR></P>
<PRE>% cd /home/bill
% cp *.txt /home/john
% cd /home/john</PRE>
<P>The syntax for grouping commands is:
<BR></P>
<PRE>( commands )</PRE>
<P>Enclosing a list of commands in parentheses is a way to override the default precedence rules for the &&, ||, and | operators, at the expense of invoking a subshell and losing any environmental effects of the commands' execution. For example,
(grep || echo) | pr will pipe the output of the grep command, and possibly that of echo if grep sets a nonzero exit code, to the pr command.
<BR></P>
<P>I/O redirections can be appended to the subshell just as for a simple command; the redirections are in effect for all of the commands within the subshell. For example, (cat; echo; date) > out will write the output of the cat, echo, and date commands
to a file named out without any breaks. If you look at the file afterward, first you'll see the lines written by cat, followed by the lines written by echo, and finally the lines written by date. Similarly, input redirections apply to all commands in the
subshell, so that each command in turn reads lines from the redirected file, starting with the line following those read by any previously executed commands in the subshell.
<BR></P>
<H5 ALIGN="CENTER">
<CENTER><A ID="I16" NAME="I16">
<FONT SIZE=3><B>Executing Commands Conditionally</B>
<BR></FONT></A></CENTER></H5>
<P>Compound commands are actually two or more commands combined together so that the shell executes all of them before prompting (or, in the case of shell scripts, reading) more input.
<BR></P>
<P>Compound commands are not often needed for work at the keyboard, and you'll rarely feel the lack if you don't understand or don't use compound commands. However, compound commands form a very useful extension to the shell's syntax, especially in shell
scripts. Some compound command formats, such as & (background job) and | (the pipe operator) are essential to effective work with UNIX.
<BR></P>
<H6 ALIGN="CENTER">
<CENTER>
<FONT SIZE=3><B>Conditional Execution on Success—</B><B><I>&&</I></B><B> (And)</B>
<BR></FONT></CENTER></H6>
<P>The double ampersand operator (read and) is used to join two commands: <I>command1</I> && <I>command2</I>. It causes the shell to execute command2 only if command1 is successful (has a zero exit code).
<BR></P>
<P>For command1 or command2, you can write a simple command or a compound command. The && operator has higher precedence than || but lower precedence than |. For example,
<BR></P>
<PRE>grep '#include' *.c | pr && echo OK</PRE>
<P>will echo OK only if the pipeline grep | pr sets a zero exit code. (For pipelines, the exit code is the exit code of the last command in the pipeline.)
<BR></P>
<P>The compound command cp file1.c file1.bak && rm file1.c shows the possible benefit of using &&: The rm command will delete file1.c only if it is first successfully copied to file1.bak.
<BR></P>
<H6 ALIGN="CENTER">
<CENTER>
<FONT SIZE=3><B>Conditional Execution on Failure —</B><B><I>||</I></B><B> (Or)</B>
<BR></FONT></CENTER></H6>
<P>The or operator is used to join two commands: <I>command1</I> || <I>command2</I>. It causes the shell to execute command2 only if command1 failed (set a nonzero exit code).
<BR></P>
<P>For command1 or command2, you can write a simple command or a compound command. The || operator has lower precedence than both the && and | operators. For example, in the following command
<BR></P>
<PRE>grep '#include' *.c || echo No files | pr</PRE>
<P>either grep succeeds, or else the words No files are piped to the pr command. That is, the pipe is between the echo and pr commands, not between grep (or grep || echo) and pr.
<BR></P>
<P>Use the || operator to provide an alternative action. For example, in the following case, if the mkdir command fails, the exit command prevents further execution of the shell script:
<BR></P>
<PRE>mkdir $tmpfile || exit</PRE>
<H4 ALIGN="CENTER">
<CENTER><A ID="I17" NAME="I17">
<FONT SIZE=3><B>Shell Statements—A Closer Look</B>
<BR></FONT></A></CENTER></H4>
<P>A command is either a basic command, or a basic command embellished with one or more I/O redirections.
<BR></P>
<P>A basic command is a series of words, each subject to replacements, which when fully resolved specifies an action to be executed and provides zero or more options and arguments to modify or control the action taken. The first word of a basic command,
sometimes called the command name, must specify the required action.
<BR></P>
<P>In plainer terms, a statement is the smallest executable unit. When the shell is operating in interactive mode, it displays its prompt when it requires a statement. You must continue to enter shell statement components, using multiple lines if
necessary, until you have completed a full statement. If the statement is not completed on one line, the shell will continue to prompt you, without executing the line or lines you have entered, until it has received a full statement.
<BR></P>
<P>Shell statements are formed from a number of tokens. A token is a basic syntactic element and can be any of the following:
<BR></P>
<UL>
<LI>Comments. A comment begins with any word having a pound sign (#) as its first character, and extends to the end of the line. This interpretation can be avoided by enclosing the pound sign (or the entire word) in quotes. (See "Quoting and Escaping
Special Characters" later in this chapter.)
<BR>
<BR></LI>
<LI>White space. White space consists of blanks and tabs, and sometimes the newline character. White space is used to separate other tokens which, if run together, would lose their separate identity. Units of text separated by white space are generically
called words.
<BR>
<BR></LI>
<LI>Statement delimiters. Statement delimiters include the semicolon (;) and the newline character (generated when you press return). You can use the semicolon to run commands together on the same line. The shell treats the commands as if they had been
entered on separate lines.
<BR>
<BR> Normally every command or shell statement ends at the end of the line. The return (or Enter) key you press to end the line generates a character distinct from printable characters, blanks and tabs, which the shell sees as a newline character. Some
statements require more than one line of input, such as the if and while commands. The syntax description for these commands shows how they should be split over lines; the line boundaries must be observed, and you must end each line at the indicated place
or else you will get a syntax error.
<BR>
<BR></LI>
<LI>Operators. An operator is a special character, or a combination of special characters, to which the shell attaches special syntactic significance. Operators shown as a combination of special characters must be written without white space between them,
otherwise they will be seen as two single operators instead of the two-character operator.
<BR>
<BR>Punctuation characters having special significance to the shell must be enclosed in quotes to avoid their special interpretation. For example, the command grep '#' *.c uses quotes to hide the pound sign from the shell so that the pound sign can be
passed to grep as an argument. See the section later in this chapter titled "Quoting and Escaping from Special Characters" for details about using quotes.
<BR>
<BR></LI>
<LI>Words. A word is any consecutive sequence of characters occurring between white space, statement delimiters, or operators. A word can be a single group of ordinary characters, a quoted string, a variable reference, a command substitution, a history
substitution, or a filename pattern; it can also be any combination of these elements. The final form of the word is the result of all substitutions and replacements, together with all ordinary characters, run together to form a single string. The string
is then used as the command name or command argument during command execution.
<BR>
<BR></LI></UL>
<H4 ALIGN="CENTER">
<CENTER><A ID="I18" NAME="I18">
<FONT SIZE=3><B>Filename Substitutions (Globbing)</B>
<BR></FONT></A></CENTER></H4>
<P>Filename generation using patterns is an important facility of the Bourne shell. The C shell supports the filename patterns of the Bourne shell and adds the use of {} (braces) to allow greater flexibility.
<BR></P>
<P>Several shell commands and contexts allow the use of pattern-matching strings, such as the case statement of switch and the =~ and !~ expression operators. In these cases, pattern strings are formed using the same rules as for filename generation,
except that the patterns are matched to another string.
<BR></P>
<P>When any of the pattern expressions described below are used as arguments of a command, the entire pattern string is replaced with the filenames or pathnames that match the pattern. By default, the shell searches the current directory for matching
filenames, but if the pattern string contains slashes (/), it searches the specified directory or directories instead. Note that several directories can be searched for matching files in a single pattern string: a pattern of the form dir/*/*.c will search
all the directories contained in dir for files ending with .c.
<BR></P>
<TABLE BORDER>
<TR>
<TD>
<P>*</P>
<TD>
<P>The asterisk matches any string of characters, including a null string. Used by itself, it matches all filenames. Used at the beginning of a pattern string, it means that leading prefixes of the filename pattern are ignored: *.c matches any filename
ending with .c. Used at the end of a pattern string, it means that trailing suffixes of the filename pattern are ignored: s.* will match s.main, s.prog.c, and any filename beginning with s.. Used in the middle of a pattern, it means that matching filenames
must begin and end as shown but can contain any character sequences in the middle: pay*.c matches filenames beginning with pay and ending with .c, such as payroll.c, paymast.c, and paycheck.c.</P>
<TR>
<TD>
<P>?</P>
<TD>
<P>The question mark matches any one character. For example, ? as a complete word will match all filenames one character long in the current directory. The pattern pay?.c will match pay1.c and pay2.c but not payroll.c. Multiple question marks can be used
to indicate a specific number of don't-care positions in the filename: pay??.c will match filenames beginning with pay and containing any two characters before .c, such as pay01.c and paybb.c, but will not match payroll.c.</P>
<TR>
<TD>
<P>[]</P>
<TD>
<P>The square brackets enclose a list of characters. Matching filenames contain one of the indicated characters in the corresponding position of the filename. For example, [abc]* will match any filename beginning with the letter a, b, or c. Because of the
asterisk, the first character can be followed by any sequence of characters.</P>
<TR>
<TD>
<P><BR></P>
<TD>
<P>Use a hyphen (-) to indicate a range of characters. For example, pay[1-3].c will match filenames pay1.c, pay2.c, and pay3.c, but not pay4.c or pay11.c. Multiple ranges can be used in a single bracketed list. For example, [A-Za-z0-9]* will match any
filename beginning with a letter or a digit. To match a hyphen, list the hyphen at the beginning or end of the character list: [-abc] or [abc-] will match an a, b, c, or hyphen.</P>
<TR>
<TD>
<P><BR></P>
<TD>
<P>Use a circumflex (^) after [ to negate the range of characters. The pattern [^a-zA-Z0-9]* will match all filenames that do not begin with a letter or digit—that is, filenames beginning with a punctuation character such as .c or #myfile.txt.</P>
<TR>
<TD>
<P>{}</P>
<TD>
<P>Braces enclose a list of patterns separated by commas. The brace expression matches filenames having any one of the listed patterns in the corresponding position of the name. For example, the pattern /usr/home/{kookla,fran,ollie}/.profile expands to the
path list /usr/home/kookla/.profile /usr/home/fran/.profile /usr/home/ollie/.profile. Unlike *, ?, and [], brace-enclosed lists are not matched against existing filenames; they are simply expanded into filenames regardless of whether the corresponding
files exist. Brace-enclosed lists can be nested, for example /usr/{bin,lib,home/{john,bill}} refers to any of the directories /usr/bin, /usr/lib, /usr/home/john, and /usr/home/bill.</P></TABLE>
<P>The tilde (~) can be used at the beginning of a word to invoke directory substitution. The tilde forms are as follows:
<BR></P>
<TABLE BORDER>
<TR>
<TD>
<P>~</P>
<TD>
<P>Substituted with the full pathname of your home directory. Also used in the form ~/<I>path</I> to refer to a file or directory under your home directory.</P>
<TR>
<TD>
<P>~<I>name</I></P>
<TD>
<P>Substituted with the full pathname of user name's home directory. For example, ~ken/bin refers to /usr/ken/bin if the home directory for user ken is /usr/ken. The password file /etc/passwd is searched for name to determine the directory pathname; if
name is not found, the shell generates an error message and stops.</P></TABLE>
<P>If the tilde does not appear by itself as a word, and is not followed by a letter or by a slash, or appears in any position other than the first, it is not replaced. Thus, /usr/marta/~file.c is a reference to the file ~file.c in the directory
/usr/marta.
<BR></P>
<P>It is important to realize that filename generation using pattern strings causes a replacement of one word with many. A filename pattern must be a single word. The ordinary characters and pattern-matching characters in the word describe a rule for
choosing filenames from the current or specified directory. The word is replaced with each filename or pathname found that matches the pattern. Consider the following examples:
<BR></P>
<PRE>% echo Files: *.txt
Files: ch1.txt ch2.txt chlast.txt
% set files=(*.txt)
% echo Found $#files files
Found 3 files
% echo $files[2]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -