⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unx11.htm

📁 Unix Unleashed, Third Edition is written with the power user and system administrator in mind. This
💻 HTM
📖 第 1 页 / 共 5 页
字号:

<PRE>$ command<B> -</B>options filenames</PRE>

<P>Although the shell does not interpret the arguments of the command, the shell does make some interpretation of the input line before passing the arguments to the command. Special characters, when you enter them on a command line, cause the shell to 
redirect input and output, start a different command, search the directories for filename patterns, substitute variable data, and substitute the output of other commands.

<BR></P>

<H5 ALIGN="CENTER">

<CENTER><A ID="I8" NAME="I8">

<FONT SIZE=3><B>Redirection of Input and Output</B>

<BR></FONT></A></CENTER></H5>

<P>When the shell sees the input (&lt;) or output (&gt;) redirection characters, the argument following the redirection symbol is sent to the subshell that controls the execution of the command. When the command opens the input or output file that has been 

redirected, the input or output is redirected to the file.

<BR></P>

<PRE>$ ls -l &gt;dirfile</PRE>

<P>In this example, the only argument passed on to ls is the option -l. The filename dirfile is sent to the subshell that controls the execution of ls. You can find more detail on input and output redirection in Chapter 4, &quot;Listing Files.&quot;

<BR></P>

<H5 ALIGN="CENTER">

<CENTER><A ID="I9" NAME="I9">

<FONT SIZE=3><B>Entering Multiple Commands on One Line</B>

<BR></FONT></A></CENTER></H5>

<P>Ordinarily, the shell interprets the first word of command input as the command name and the rest of the input as arguments to that command. Three shell special characters&#151;the semicolon (;), the ampersand (&amp;), and the vertical bar (|) or 
pipe&#151;direct the shell to interpret the word following the symbol as a new command, with the rest of the input as arguments to the new command. For example, the command line

<BR></P>

<PRE>$ who -H; df -v; ps -e</PRE>

<P>is the equivalent of

<BR></P>

<PRE>$ who -H

$ df -v

$ ps -e</PRE>

<P>In the second case, however, the results of each command appear between the command input lines. When you use the semicolon to separate commands on a line, the commands are executed in sequence. The shell waits until one command is complete before 
executing the next.

<BR></P>

<P>If you separate commands on a line using the ampersand (&amp;), the shell does not wait until one command is run before the second is started. If the ampersand is the last character on the input line, the last command is executed as a background job. To 

run the preceding series of commands concurrently, you enter the following:

<BR></P>

<PRE>$ who -H &amp; df -v &amp; ps -e</PRE>

<P>Whereas the semicolon serves merely as a command separator, the pipe symbol serves a different purpose. When the shell sees the pipe symbol, it takes the next word as a new command and redirects the standard output of the prior command to the standard 
input of the new command. For example, the command line

<BR></P>

<PRE>$ who | sort</PRE>

<P>displays an alphabetized list of all logged-in users. The command line

<BR></P>

<PRE>$ who | sort | lp</PRE>

<P>prints a hard copy of the alphabetized list of all logged-in users. You can find more information on pipelines in Chapter 4, &quot;Listing Files.&quot;

<BR></P>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="imp.gif" WIDTH = 68 HEIGHT = 35><B>TIP:</B> When you're using pipelines, sometimes the order of the commands does not make a difference in the output, but it might make a difference in how efficiently the pipeline executes. The two commands

<BR>

<BR>sort /etc/inittab | grep bin/sh

<BR>grep bin/sh /etc/inittab | sort

<BR>

<BR>accomplish the same thing, but the second pipeline operates more efficiently because it reduces the amount of data passed to sort.

<BR></NOTE>

<HR ALIGN=CENTER>

<H5 ALIGN="CENTER">

<CENTER><A ID="I10" NAME="I10">

<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. You can accomplish by escaping the newline character.

<BR></P>

<P>Remember that the shell sees a line of input as a string of characters 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, which is 

the newline character, is treated literally, meaning that the shell does not interpret the newline character as the end of the line of input. For example,

<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="I11" NAME="I11">

<FONT SIZE=3><B>Filename Substitutions on the Command Line</B>

<BR></FONT></A></CENTER></H5>

<P>Although the command separator, the pipe symbol, and the redirection symbols change the operational effects of a command line, they did not affect the arguments that were passed to the command. The substitution characters, on the other hand, cause a 
substitution to take place in the stream of arguments passed to a command. The most common substitution is filename substitution. When the shell's command-line interpreter sees one of the metacharacters&#151;the asterisk (*), the question mark (?), or 
square brackets ([,])&#151;the shell searches the directories for filenames that match a pattern indicated by the metacharacter.

<BR></P>

<P>The asterisk special character causes the shell to search the directory for filenames that match any pattern. The command

<BR></P>

<PRE>$ ls f*

file1

file1a

form</PRE>

<P>creates a listing of all filenames beginning with the letter f. The important point here is that the shell, not the ls command, did the directory search. In the following example, the ls command sees three arguments, and the preceding command line is 
the equivalent of

<BR></P>

<PRE>$ ls file1 file1a form

file1

file1a

form</PRE>

<P>The shell makes filename substitutions regardless of the command to be executed.

<BR></P>

<PRE>$ echo f*

file1 file1a form</PRE>

<P>The question mark metacharacter searches the directories for filenames that match the pattern with any single character substituted for the metacharacter. Square brackets cause a match to be made on any character appearing within the brackets. You can 
find more details on filename substitution in Chapter 4, &quot;Listing Files.&quot;

<BR></P>

<H5 ALIGN="CENTER">

<CENTER><A ID="I12" NAME="I12">

<FONT SIZE=3><B>Substitution of Variable Data</B>

<BR></FONT></A></CENTER></H5>

<P>The second type of substitution that can take place is variable substitution. When the shell sees the dollar sign ($) character, the remainder of the word following the dollar sign is presumed to be a variable name. The shell then searches for any 
variables that have been defined for the current shell and substitutes the value of the variable in the command line. If the variable has not been defined, a null string, one containing no characters, is substituted on the command line. For example, the 
command

<BR></P>

<PRE>$ ls $HOME</PRE>

<P>lists the contents of the users' home directory, regardless of what the current working directory is. HOME is an environment variable. Variables are discussed in more detail in the next major section of this chapter. As in filename substitution, the ls 

command sees only the result of the substitution, not the variable name.

<BR></P>

<P>You can substitute variable names anywhere in the command line, including for the command name itself. For example,

<BR></P>

<PRE>$ dir=ls

$ $dir f*

file1

file1a

form</PRE>

<P>This example points out that the shell makes its substitutions before determining what commands to execute.

<BR></P>

<H5 ALIGN="CENTER">

<CENTER><A ID="I13" NAME="I13">

<FONT SIZE=3><B>Substituting the Results of Commands in a Command Line</B>

<BR></FONT></A></CENTER></H5>

<P>Sometimes it is useful to pass the output or results of one command as arguments to another command. You do so by using the shell special character, the back quotation mark ('). You use the back quotation marks in pairs. When the shell sees a pair of 
back quotation marks, it executes the command inside the quotation marks and substitutes the output of that command in the original command line. You most commonly use this method to store the results of command executions in variables. To store the 
five-digit Julian date in a variable, for example, you use the following command:

<BR></P>

<PRE>$ julian='date '+%y%j''</PRE>

<P>The back quotation marks cause the date command to be executed before the variable assignment is made. Back quotation marks can be extremely useful when you're performing arithmetic on shell variables; see &quot;Shell Programming&quot; later in this 
chapter.

<BR></P>

<H5 ALIGN="CENTER">

<CENTER><A ID="I14" NAME="I14">

<FONT SIZE=3><B>Escaping from the Special Characters</B>

<BR></FONT></A></CENTER></H5>

<P>By now, it should be clear that the shell looks for special characters in the input line before taking any other action. When it becomes necessary to use a special character in a command line, you can override, or &quot;escape,&quot; the special action 

of the character by using an escape character. The escape characters are:

<BR></P>

<TABLE BORDER>

<TR>

<TD>

<P>\</P>

<TD>

<P>the backslash, which causes a single character to be escaped</P>

<TR>

<TD>

<P>'</P>

<TD>

<P>the single quotation mark, which, used in pairs, causes a group of characters to be escaped</P>

<TR>

<TD>

<P>&quot;</P>

<TD>

<P>the double quotation mark, which, used in pairs, causes a group of characters to be escaped, but allows some special characters to perform their normal function</P></TABLE>

<P>For example, UNIX does not forbid you to use special characters in filenames.

<BR></P>

<P>Suppose you have a directory with the following files:

<BR></P>

<PRE>file* file1 file2 file3</PRE>

<P>You want to display the contents of the first file, the one with the asterisk in its name. Enter the following command:

<BR></P>

<PRE>$ cat file*</PRE>

<P>You then get not only the file you want, but the rest of the files in the directory as well. Because you now understand how the shell interprets its input, performing filename substitution whenever it sees an asterisk, you know what the problem is but 
wonder how you can rectify the problem. If you try to remove the offending file with the command

<BR></P>

<PRE>$ rm file*</PRE>

<P>you remove far more than you want. Fortunately, the Bourne shell has provided a mechanism to get around this kind of problem.

<BR></P>

<P>Another special character that the shell looks for is the escape character, or backslash (\). When the shell sees the escape character, it takes the subsequent character literally; it does not attempt to interpret that character. In the preceding 
scenario, you can use the escape character to remove or even rename the offending file:

<BR></P>

<PRE>$ mv file\* filestar</PRE>

<P>Other characters that get special treatment by the shell are the white space characters. They are the tabs, spaces, and newlines, which make a separation between words. When more than one of these characters appears at a time, the shell strips the 
redundant white space characters from the input. For example,

<BR></P>

<PRE>$ echo This      word         is separated

This word is separated</PRE>

<P>Here, the result is probably not the desired effect.

<BR></P>

<P>Suppose you want to display a message with asterisks as attention getters, as in the following:

<BR></P>

<PRE>***** Program Error *****</PRE>

<P>To do so using escape characters would be cumbersome, as you can see in the following:

<BR></P>

<PRE>$ echo \*\*\*\*\* Program Error \*\*\*\*\*

***** Program Error *****</PRE>

<P>You may already have guessed that the shell has an easier way of displaying a message of this type. When you enclose a string of text within single quotation marks ('), the entire string is treated literally, as follows:

<BR></P>

<PRE>$ echo '***** Program Error *****'

***** Program Error *****</PRE>

<P>You can use this same method to retain white space:

<BR></P>

<PRE>$ echo 'This     word       is separated'

This     word       is separated</PRE>

<P>On some occasions, however, you may want part of a string to be treated literally and other parts to be interpreted by the shell.

<BR></P>

<PRE>$ USERS='who | wc -l'

$ echo '*** There are $USERS users logged into the system'

*** There are $USERS users logged into the system</PRE>

<P>You can overcome this problem by using the double quotation marks (&quot;). The double quotation marks are not as restrictive as the single quotation marks. Within double quotation marks, the dollar sign ($), the backslash (\), and the back quotation 
marks (`) retain their shell meaning:

<BR></P>

<PRE>$ USERS='who | wc -l'

$ echo &quot;*** There are $USERS users logged into the system&quot;

There are 5 users logged into the system</PRE>

<P>Because the double quotation marks still allow the shell to interpret the back quotation marks, you can simplify the preceding example, as follows:

<BR></P>

<PRE>$ echo &quot;*** There are `who | wc -l` users logged into the system&quot;

There are 5 users logged into the system</PRE>

<H4 ALIGN="CENTER">

<CENTER><A ID="I15" NAME="I15">

<FONT SIZE=3><B>Entering Data from the Shell</B>

<BR></FONT></A></CENTER></H4>

<P>One of the useful features of a computer is its ability to process large volumes of data at one time. Often this data exists in the form of disk files. You have seen how you can provide a file name to a UNIX program in a command line, but what if the 
data does not exist in a file? For instance, you can use the UNIX mail utility to send a data file to another user, but often you just want to type in a short note to send to another user. Many UNIX programs allow data to be supplied to the program either 

from a disk file or from your keyboard.

<BR></P>

<P>When a UNIX program needs more data from the user than is practical to get from a command line, the program issues a read request. For most programs, a read terminates when a newline character is processed. In other words, a read request processes one 
line at a time. When the program is reading its data from a file, no response from the user is necessary for a read request, but when the program is reading its data from the keyboard, or standard input, the program pauses until the user enters a string of 

characters terminated with a newline character. Consider the following example:

<BR></P>

<PRE>$ head -2

Line 1

Line 1

Line 2

Line 2

$</PRE>

<P>Because no filename is supplied on the command line, and no input redirection has occurred, head is looking for its input from the standard input. After you enter the first line, head processes the line and sends it to the output file, which in this 
case is the standard output, creating the echo effect of the example. After you enter the second line, head displays that line and terminates because the command-line option requested only two lines. The natural operation of some programs is to process a 
file until it has processed the entire file. When a program looks for a complete file, and the file comes from the standard input, the user needs some way to indicate the end-of-data or end-of-file condition. In the Bourne shell, the end-of-file is 
indicated by Ctrl+d.

<BR></P>

<P>The file concatenation utility cat, processes an entire file at one time. In the following example, cat &quot;sees&quot; a file containing two lines.

<BR></P>

<PRE>$ <B>cat</B>

Line 1

Line 1

Line 2

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -