📄 ch13.htm
字号:
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<H1></H1>
<UL>
<LI><A HREF="#Heading1">- 13 -</A>
<UL>
<LI><A HREF="#Heading2">Shell Programming</A>
<UL>
<LI><A HREF="#Heading3">Creating and Running Shell Programs</A>
<LI><A HREF="#Heading4">NOTE</A>
<LI><A HREF="#Heading5">NOTE</A>
<LI><A HREF="#Heading6">Using Variables</A>
<UL>
<LI><A HREF="#Heading7">Assigning a Value to a Variable</A>
</UL>
<LI><A HREF="#Heading8">NOTE</A>
<UL>
<LI><A HREF="#Heading9">Accessing the Value of a Variable</A>
</UL>
<LI><A HREF="#Heading10">NOTE</A>
<UL>
<LI><A HREF="#Heading11">Positional Parameters and Other Built-In Shell Variables</A>
</UL>
<LI><A HREF="#Heading12">The Importance of Quotation Marks</A>
<LI><A HREF="#Heading13">NOTE</A>
<LI><A HREF="#Heading14">NOTE</A>
<LI><A HREF="#Heading15">The test Command</A>
<UL>
<LI><A HREF="#Heading16">The tcsh Equivalent of the test Command</A>
</UL>
<LI><A HREF="#Heading17">Conditional Statements</A>
<UL>
<LI><A HREF="#Heading18">The if Statement</A>
</UL>
<LI><A HREF="#Heading19">NOTE</A>
<LI><A HREF="#Heading20">NOTE</A>
<LI><A HREF="#Heading21">NOTE</A>
<UL>
<LI><A HREF="#Heading22">The case Statement</A>
</UL>
<LI><A HREF="#Heading23">Iteration Statements</A>
<UL>
<LI><A HREF="#Heading24">The for Statement</A>
</UL>
<LI><A HREF="#Heading25">NOTE</A>
<UL>
<LI><A HREF="#Heading26">The while Statement</A>
<LI><A HREF="#Heading27">The until Statement</A>
</UL>
<LI><A HREF="#Heading28">NOTE</A>
<UL>
<LI><A HREF="#Heading29">The shift Command</A>
</UL>
<LI><A HREF="#Heading30">NOTE</A>
<UL>
<LI><A HREF="#Heading31">The select Statement</A>
<LI><A HREF="#Heading32">The repeat Statement</A>
</UL>
<LI><A HREF="#Heading33">NOTE</A>
<LI><A HREF="#Heading34">Functions</A>
<LI><A HREF="#Heading35">NOTE</A>
<LI><A HREF="#Heading36">Summary</A>
</UL>
</UL>
</UL>
<P>
<HR SIZE="4">
<H2 ALIGN="CENTER"><A NAME="Heading1<FONT COLOR="#000077">- 13 -</FONT></H2>
<H2 ALIGN="CENTER"><A NAME="Heading2<FONT COLOR="#000077">Shell Programming</FONT></H2>
<P><I>by Rick McMullin</I></P>
<P>IN THIS CHAPTER</P>
<UL>
<LI>Creating and Running Shell Programs
<P>
<LI>Using Variables
<P>
<LI>The Importance of Quotation Marks
<P>
<LI>The test Command
<P>
<LI>Conditional Statements
<P>
<LI>Iteration Statements
<P>
<LI>Functions
</UL>
<P><BR>
The last 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.</P>
<P>This chapter describes the fundamentals of shell programming and compares the
<TT>bash</TT>, <TT>pdksh</TT>, and <TT>tcsh</TT> programming languages. This chapter
covers the following topics:
<UL>
<LI>Creating and running shell programs
<P>
<LI>Using shell variables
<P>
<LI>The importance of quotes
<P>
<LI>The <TT>test</TT> command
<P>
<LI>Conditional statements
<P>
<LI>Iteration statements
</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.
<H3 ALIGN="CENTER"><A NAME="Heading3<FONT COLOR="#000077">Creating and Running
Shell Programs</FONT></H3>
<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.</P>
<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 <TT>umount</TT>
command, and then remount the drive using the Linux <TT>mount</TT> command. This
sequence of steps is shown by the following commands:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">umount /dev/cdrom
</FONT></PRE>
<PRE><FONT COLOR="#0066FF">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 <TT>remount</TT> (or
any other name you want).</P>
<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:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">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 <TT>remount</TT> on the command
line.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading4<FONT COLOR="#000077"><B>NOTE: </B></FONT>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 <TT>tcsh</TT> to write
programs, the first line of the shell program must start with a <TT>#</TT> for <TT>tcsh</TT>
to recognize it as a <TT>tcsh</TT> program file.
<HR>
</DL>
<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 <TT>tcsh</TT>
program, this is done by entering the following command:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">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.</P>
<P>A third way of executing the commands in a shell program file is to use the <TT>.</TT>
command (in <TT>pdksh</TT> and <TT>bash</TT>) and the <TT>source</TT> command in
<TT>tcsh</TT>. 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 <TT>bash</TT> or <TT>pdksh</TT> to execute the commands in the
<TT>remount</TT> file:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">. remount
</FONT></PRE>
<P>To do the same thing in <TT>tcsh</TT>, you would type the following command:<FONT
COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">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:<FONT
COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">mount -t msdos /dev/fd0 /a
cp file1 /a
cp file2 /a
cp file3 /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 four 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.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading5<FONT COLOR="#000077"><B>NOTE: </B></FONT>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.
<HR>
</DL>
<H3 ALIGN="CENTER"><A NAME="Heading6<FONT COLOR="#000077">Using Variables</FONT></H3>
<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 <TT>PATH</TT> variable and the <TT>PS1</TT> 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.
<H4 ALIGN="CENTER"><A NAME="Heading7<FONT COLOR="#000077">Assigning a Value
to a Variable</FONT></H4>
<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 <TT>count</TT>, you would enter the following command in <TT>bash</TT> or
<TT>pdksh</TT>:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">count=5
</FONT></PRE>
<P>With <TT>tcsh</TT> you would have to enter the following command to achieve the
same results:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">set count = 5
</FONT></PRE>
<DL>
<DT><FONT COLOR="#0066FF"></FONT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading8<FONT COLOR="#000077"><B>NOTE: </B></FONT>With the <TT>bash</TT>
and <TT>pdksh</TT> syntax for setting a variable, you must make sure that there are
no spaces on either side of the equal sign. With <TT>tcsh</TT>, it doesn't matter
whether there are spaces.
<HR>
</DL>
<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: <TT>name=Garry</TT>
(for <TT>pdksh</TT> and <TT>bash</TT>)
<BLOCKQUOTE>
<P><TT>set name = Garry</TT> (for <TT>tcsh</TT>)
</BLOCKQUOTE>
<H4 ALIGN="CENTER"><A NAME="Heading9<FONT COLOR="#000077">Accessing the Value
of a Variable</FONT></H4>
<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 (<TT>$</TT>).
If you wanted to print the value stored in the <TT>count</TT> variable to the screen,
you would do so by entering the following command:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">echo $count
</FONT></PRE>
<DL>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -