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

📄 unx10.htm

📁 Linux Unix揭密.高质量电子书籍.对学习Linux有大帮助,欢迎下载学习.
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<LI>Maintenance of variables

<BR>

<BR></LI>

<LI>Environment control

<BR>

<BR></LI>

<LI>Shell programming

<BR>

<BR></LI></UL>

<H4 ALIGN="CENTER">

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

<FONT SIZE=3><B>Command Line Interpretation</B>

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

<P>When you log in, starting a special version of a shell called an interactive shell, you see a shell prompt, usually in the form of a dollar sign ($), a percent sign (%), or a pound sign (#). When you type a line of input at a shell prompt, the shell 
tries to interpret it. Input to a shell prompt is sometimes called a command line. The basic format of a command line is

<BR></P>

<PRE>command arguments</PRE>

<P><I>command</I> is an executable UNIX command, program, utility, or shell program. The <I>arguments</I> are passed to the executable. Most UNIX utility programs expect <I>arguments</I> to take the following form:

<BR></P>

<PRE>options filenames</PRE>

<P>For example, in the command line

<BR></P>

<PRE>$ ls -l file1 file2</PRE>

<P>there are three arguments to ls, the first of which is an option, while the last two are file names.

<BR></P>

<P>One of the things the shell does for the kernel is to eliminate unnecessary information. For a computer, one type of unnecessary information is whitespace; therefore, it is important to know what the shell does when it sees whitespace. Whitespace 
consists of the space character, the horizontal tab, and the new line character. Consider this example:

<BR></P>

<PRE>$ echo part A     part B     part C

part A part B part C</PRE>

<P>Here, the shell has interpreted the command line as the echo command with six arguments and has removed the whitespace between the arguments. For example, if you were printing headings for a report and you wanted to keep the whitespace, you would have 
to enclose the data in quotation marks, as in

<BR></P>

<PRE>$ echo 'part A     part B     part C'

part A     part B     part C</PRE>

<P>The single quotation mark prevents the shell from looking inside the quotes. Now the shell interprets this line as the echo command with a single argument, which happens to be a string of characters including whitespace.

<BR></P>

<H4 ALIGN="CENTER">

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

<FONT SIZE=3><B>Program Initiation</B>

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

<P>When the shell finishes interpreting a command line, it initiates the execution of the requested program. The kernel actually executes it. To initiate program execution, the shell searches for the executable file in the directories specified in the PATH 

environment variable. When it finds the executable file, a subshell is started for the program to run. You should understand that the subshell can establish and manipulate its own environment without affecting the environment of its parent shell. For 
example, a subshell can change its working directory, but the working directory of the parent shell remains unchanged when the subshell is finished.

<BR></P>

<H4 ALIGN="CENTER">

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

<FONT SIZE=3><B>Input-output Redirection</B>

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

<P>Chapter 4, &quot;Listing Files,&quot; introduced input-output redirection. It is the responsibility of the shell to make this happen. The shell does the redirection before it executes the program. Consider these two examples, which use the wc word count 

utility on a data file with 5 lines:

<BR></P>

<PRE>$ wc -l fivelines

5 fivelines

$ wc -l &lt;fivelines

5</PRE>

<P>This is a subtle difference. In the first example, wc understands that it is to go out and find a file named fivelines and operate on it. Since wc knows the name of the file it displays it for the user. In the second example, wc sees only data, and does 

not know where it came from because the shell has done the work of locating and redirecting the data to wc, so wc cannot display the file name.

<BR></P>

<H4 ALIGN="CENTER">

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

<FONT SIZE=3><B>Pipeline Connection</B>

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

<P>Since pipeline connections are actually a special case of input-output redirection in which the standard output of one command is piped directly to the standard input of the next command, it follows that pipelining also happens before the program call 
is made. Consider this command line:

<BR></P>

<PRE>$ who | wc -l

5</PRE>

<P>In the second example, rather than displaying its output on your screen, the shell has directed the output of who directly to the input of wc. Pipes are discussed in Chapter 4.

<BR></P>

<H4 ALIGN="CENTER">

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

<FONT SIZE=3><B>Substitution of Filenames</B>

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

<P>Chapter 4 explained how metacharacters can be used to reference more than one file in a command line. It is the responsibility of the shell to make this substitution. The shell makes this substitution before it executes the program. For example,

<BR></P>

<PRE>$ echo *

file1 file2 file3 file3x file4</PRE>

<P>Here, the asterisk is expanded to the five filenames, and it is passed to echo as five arguments. If you wanted to echo an asterisk, we would enclose it in quotation marks.

<BR></P>

<H4 ALIGN="CENTER">

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

<FONT SIZE=3><B>Maintenance of Variables</B>

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

<P>The shell is capable of maintaining variables. Variables are places where you can store data for later use. You assign a value to a variable with an equal (=) sign.

<BR></P>

<PRE>$ LOOKUP=/usr/mydir</PRE>

<P>Here, the shell establishes LOOKUP as a variable, and assigns it the value /usr/mydir. Later, you can use the value stored in LOOKUP in a command line by prefacing the variable name with a dollar sign ($). Consider these examples:

<BR></P>

<PRE>$ echo $LOOKUP

/usr/mydir

$ echo LOOKUP

LOOKUP</PRE>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>Note for C-shell users.</B>  Assigning values to variables in the C-shell differs from the Bourne and Korn shells.  To assign a variable in the C-shell use the set command.

<BR>

<BR>% set LOOKUP = /usr/mydir

<BR>

<BR>Notice that spaces precede and follow the equal sign.

<BR></NOTE>

<HR ALIGN=CENTER>

<P>Like filename substitution, variable name substitution happens before the program call is made. The second example omits the dollar sign ($). Therefore, the shell simply passes the string to echo as an argument. In variable name substitution, the value 

of the variable replaces the variable name.

<BR></P>

<P>For example, in

<BR></P>

<PRE>$ ls $LOOKUP/filename</PRE>

<P>the ls program is called with the single argument /usr/mydir/filename.

<BR></P>

<H4 ALIGN="CENTER">

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

<FONT SIZE=3><B>Environment Control</B>

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

<P>When the login program invokes your shell, it sets up your environment, which includes your home directory, the type of terminal you are using, and the path that will be searched for executable files. The environment is stored in variables called 
environmental variables. To change the environment, you simply change a value stored in an environmental variable. For example, to change the terminal type, you change the value in the TERM variable, as in

<BR></P>

<PRE>$ echo $TERM

vt100

$ TERM=ansi

$ echo $TERM

ansi</PRE>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>Note for C-shell users.</B>  C-shell assigns values to environment variables using the setenv command.

<BR>

<BR>% setenv TERM vt100

<BR></NOTE>

<HR ALIGN=CENTER>

<P>Chapter 11, &quot;Bourne Shell,&quot; Chapter 12, &quot;Korn Shell,&quot; and Chapter 13, &quot;C Shell,&quot; contain more information on customizing your environment.

<BR></P>

<H4 ALIGN="CENTER">

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

<FONT SIZE=3><B>Shell Programming</B>

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

<P>You've seen that the shell is used to interpret command lines, maintain variables, and execute programs. The shell also is a programming language. By combining commands and variable assignments with flow control and decision making, you have a powerful 

programming tool. Using the shell as a programming language, you can automate recurring tasks, write reports and you can even build and manipulate your own data files. The next three chapters discuss shell programming in more detail.

<BR></P>

<H3 ALIGN="CENTER">

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

<FONT SIZE=4><B>Summary</B>

<BR></FONT></A></CENTER></H3>

<P>The shell provides an interface between the user and the heart of UNIX&#151;the kernel. The shell takes command lines as input, makes filename and variable substitution, redirects input and output, locates the executable file, and initiates programs. 
The shell maintains each user's environment variables. The shell also is a powerful programming language.

<BR></P>

<P><A HREF="unxpt2au.htm"><IMG SRC="bluprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>

<A HREF="index.htm"><IMG SRC="blutoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>

<A HREF="unx11.htm"><IMG SRC="blunext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>

<A HREF="index.htm"><IMG SRC="bluprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Home"></A>

</P></BODY></HTML>

⌨️ 快捷键说明

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