📄 rhl13.htm
字号:
<HTML>
<HEAD>
<TITLE>Red Hat Linux Unleashed rhl13.htm </TITLE>
<LINK REL="ToC" HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/index.htm">
<LINK REL="Index" HREF="htindex.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/htindex.htm">
<LINK REL="Next" HREF="rhl14.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl14.htm">
<LINK REL="Previous" HREF="rhl12.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl12.htm"></HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">
<A NAME="I0"></A>
<H2>Red Hat Linux Unleashed rhl13.htm</H2>
<P ALIGN=LEFT>
<A HREF="rhl12.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl12.htm" TARGET="_self"><IMG SRC="purprev.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>
<A HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/index.htm" TARGET="_self"><IMG SRC="purtoc.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>
<A HREF="rhl14.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl14.htm" TARGET="_self"><IMG SRC="purnext.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>
<HR ALIGN=CENTER>
<P>
<UL>
<UL>
<UL>
<LI>
<A HREF="#E68E92" >Creating and Running Shell Programs</A>
<LI>
<A HREF="#E68E93" >Using Variables</A>
<UL>
<LI>
<A HREF="#E69E186" >Assigning a Value to a Variable</A>
<LI>
<A HREF="#E69E187" >Accessing the Value of a Variable</A>
<LI>
<A HREF="#E69E188" >Positional Parameters and Other Built-In Shell Variables</A></UL>
<LI>
<A HREF="#E68E94" >The Importance of Quotation Marks</A>
<LI>
<A HREF="#E68E95" >The test Command</A>
<UL>
<LI>
<A HREF="#E69E189" >The tcsh Equivalent of the test Command</A></UL>
<LI>
<A HREF="#E68E96" >Conditional Statements</A>
<UL>
<LI>
<A HREF="#E69E190" >The if Statement</A>
<LI>
<A HREF="#E69E191" >The case Statement</A></UL>
<LI>
<A HREF="#E68E97" >Iteration Statements</A>
<UL>
<LI>
<A HREF="#E69E192" >The for Statement</A>
<LI>
<A HREF="#E69E193" >The while Statement</A>
<LI>
<A HREF="#E69E194" >The until Statement</A>
<LI>
<A HREF="#E69E195" >The shift Command</A>
<LI>
<A HREF="#E69E196" >The select Statement</A>
<LI>
<A HREF="#E69E197" >The repeat Statement</A></UL>
<LI>
<A HREF="#E68E98" >Functions</A>
<LI>
<A HREF="#E68E99" >Summary</A></UL></UL></UL>
<HR ALIGN=CENTER>
<A NAME="E66E13"></A>
<H1 ALIGN=CENTER>
<CENTER>
<FONT SIZE=6 COLOR="#FF0000"><B>13</B></FONT></CENTER></H1>
<BR>
<A NAME="E67E13"></A>
<H2 ALIGN=CENTER>
<CENTER>
<FONT SIZE=6 COLOR="#FF0000"><B>Shell Programming</B></FONT></CENTER></H2>
<BR>
<P>The previous three chapters described how to use the most common Linux shell programs. I mentioned that these shell programs have powerful interpretive programming languages built into them. Now it's time to look at them in more detail.
<BR>
<P>This chapter describes the fundamentals of shell programming and compares the bash, pdksh, and tcsh programming languages. This chapter covers the following topics:
<BR>
<UL>
<LI>Creating and running shell programs
<BR>
<BR>
<LI>Using shell variables
<BR>
<BR>
<LI>The importance of quotes
<BR>
<BR>
<LI>The test command
<BR>
<BR>
<LI>Conditional statements
<BR>
<BR>
<LI>Iteration statements
<BR>
<BR>
</UL>
<P>This chapter contains several small examples of shell programs. Each new concept or command that is introduced has some example code that further helps to explain what is being presented.
<BR>
<BR>
<A NAME="E68E92"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>Creating and Running Shell Programs</B></FONT></CENTER></H3>
<BR>
<P>At the simplest level, shell programs are just files that contain one or more shell or Linux commands. These programs can be used to simplify repetitive tasks, to replace two or more commands that are always executed together with a single command, to
automate the installation of other programs, and to write simple interactive applications.
<BR>
<P>To create a shell program, you must create a file using a text editor and put the shell or Linux commands you want to be executed into that file. For example, assume you have a CD-ROM drive mounted on your Linux system. This CD-ROM device is mounted
when the system is first started. If you later change the CD in the drive, you must force Linux to read the new directory contents. One way of achieving this is to put the new CD into the drive, unmount the CD-ROM drive using the Linux umount command, and
then remount the drive using the Linux mount command. This sequence of steps is shown by the following commands:
<BR>
<PRE>
<FONT COLOR="#000080">umount /dev/cdrom
mount -t iso9660 /dev/cdrom /cdrom</FONT></PRE>
<P>Instead of typing both of these commands each time you change the CD in your drive, you could create a shell program that would execute both of these commands for you. To do this, put the two commands into a file and call the file remount (or any other
name you want).
<BR>
<P>Several ways of executing the commands are contained in the remount file. One way to accomplish this is to make the file executable. This is done by entering the following command:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">chmod +x remount</FONT></PRE>
<P>This command changes the permissions of the file so that it is now executable. You can now run your new shell program by typing remount on the command line.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>The remount shell program must be in a directory that is in your search path, or the shell will not be able to find the program to execute. Also, if you are using tcsh to write programs, the first line of the shell program must start with a # for
tcsh to recognize it as a tcsh program file.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>Another way you can execute the shell program is to run the shell that the program was written for and pass the program in as a parameter to the shell. In a tcsh program, this is done by entering the following command:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">tcsh remount</FONT></PRE>
<P>This command starts up a new shell and tells it to execute the commands that are found in the remount file.
<BR>
<P>A third way of executing the commands in a shell program file is to use the . command (in pdksh and bash) and the source command in tcsh. This command tells the shell to execute all the commands in the file that is passed as an argument to the command.
For example, the following command can be used to tell bash or pdksh to execute the commands in the remount file:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">. remount</FONT></PRE>
<P>To do the same thing in tcsh, you would type the following command:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">source remount</FONT></PRE>
<P>Another situation in which a simple shell program can save a lot of time is described in the following example. Assume you were working on three different files in a directory, and at the end of every day you wanted to back up those three files onto a
floppy disk. To do this you would type a series of commands similar to the following:
<BR>
<PRE>
<FONT COLOR="#000080">mr dir /a
mount -t msdos /dev/fd0 /a
cp file1 /a
cp file2 /a
cp file3 /a
umount /a</FONT></PRE>
<P>As stated in the example, one way of doing this would be to mount the floppy drive and then type three copy commands, one for each file you wanted to copy. A simpler way would be to put the six commands into a text file called backup and then execute
the backup command when you wanted to copy the three files onto the floppy drive.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>You will still have to ensure that the backup shell program is executable and is in a directory that is in your path before you run the command.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E68E93"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>Using Variables</B></FONT></CENTER></H3>
<BR>
<P>As is the case with almost any language, the use of variables is very important in shell programs. You saw some of the ways in which shell variables can be used in the introductory shell chapters. Two of the variables that were introduced were the PATH
variable and the PS1 variable. These are examples of built-in shell variables, or variables that are defined by the shell program you are using. This section describes how you can create your own variables and use them in simple shell programs.
<BR>
<BR>
<A NAME="E69E186"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Assigning a Value to a Variable</B></FONT></CENTER></H4>
<BR>
<P>In all three of the shells I have discussed, you can assign a value to a variable simply by typing the variable name followed by an equal sign and the value you want to assign to the variable. For example, if you wanted to assign a value of 5 to the
variable count, you would enter the following command in bash or pdksh:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">count=5</FONT></PRE>
<P>With tcsh you would have to enter the following command to achieve the same results:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">set count = 5</FONT></PRE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>With the bash and pdksh syntax for setting a variable, you must make sure that there are no spaces on either side of the equal sign. With tcsh, it doesn't matter if there are spaces or not.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>Notice that you do not have to declare the variable as you would if you were programming in C or Pascal. This is because the shell language is a non-typed interpretive language. This means that you can use the same variable to store character strings
that you use to store integers. You would store a character string into a variable in the same way that you stored the integer into a variable. For example:
<BR>
<UL>
<UL>
<P>name=Garry - (for pdksh and bash)
<BR>
</UL></UL>
<UL>
<UL>
<P>set name = Garry - (for tcsh)
<BR>
</UL></UL>
<BR>
<A NAME="E69E187"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Accessing the Value of a Variable</B></FONT></CENTER></H4>
<BR>
<P>Once you have stored a value into a variable, how do you get the value back out? You do this in the shell by preceding the variable name with a dollar sign ($). If you wanted to print the value stored in the count variable to the screen, you would do so
by entering the following command:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">echo $count</FONT></PRE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>If you omitted the $ from the preceding command, the echo command would display the word count on-screen.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E69E188"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Positional Parameters and Other Built-In Shell Variables</B></FONT></CENTER></H4>
<BR>
<P>The shell has knowledge of a special kind of variable called a positional parameter. Positional parameters are used to refer to the parameters that were passed to a shell program on the command line or a shell function by the shell script that invoked
the function. When you run a shell program that requires or supports a number of command-line options, each of these options is stored into a positional parameter. The first parameter is stored into a variable named 1, the second parameter is stored into a
variable named 2, and so forth. These variable names are reserved by the shell so that you can't use them as variables you define. To access the values stored in these variables, you must precede the variable name with a dollar sign ($) just as you do with
variables you define.
<BR>
<P>The following shell program expects to be invoked with two parameters. The program takes the two parameters and prints the second parameter that was typed on the command line first and the first parameter that was typed on the command line second.
<BR>
<PRE>
<FONT COLOR="#000080">#program reverse, prints the command line parameters out in reverse #order
echo "$2" "$1"</FONT></PRE>
<P>If you invoked this program by entering
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">reverse hello there</FONT></PRE>
<P>the program would return the following output:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">there hello</FONT></PRE>
<P>Several other built-in shell variables are important to know about when you are doing a lot of shell programming. Table 13.1 lists these variables and gives a brief description of what each is used for.
<BR>
<BR>
<P ALIGN=CENTER>
<CENTER>
<FONT COLOR="#000080"><B>Table 13.1. Built-in shell variables.</B></FONT></CENTER>
<BR>
<TABLE BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 >
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<I>Variable</I>
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<I>Use</I></FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
$#
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Stores the number of command-line arguments that were passed to the shell program.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
$?
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Stores the exit value of the last command that was executed.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
$0
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Stores the first word of the entered command (the name of the shell program).</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
$*
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Stores all the arguments that were entered on the command line ($1 $2 ...).</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
"$@"
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Stores all the arguments that were entered on the command line, individually quoted ("$1" "$2" ...).</FONT>
</TABLE><BR>
<A NAME="E68E94"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>The Importance of Quotation Marks</B></FONT></CENTER></H3>
<BR>
<P>The use of the different types of quotation marks is very important in shell programming. Both kinds of quotation marks and the backslash character are used by the shell to perform different functions. The double quotation marks (""), the
single quotation marks (''), and the backslash (\) are all used to hide special characters from the shell. Each of these methods hides varying degrees of special characters from the shell.
<BR>
<P>The double quotation marks are the least powerful of the three methods. When you surround characters with double quotes, all the whitespace characters are hidden from the shell, but all other special characters are still interpreted by the shell. This
type of quoting is most useful when you are assigning strings that contain more than one word to a variable. For example, if you wanted to assign the string hello there to the variable greeting, you would type the following command:
<BR>
<PRE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -