261-264.html
来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 138 行
HTML
138 行
<HTML>
<HEAD>
<TITLE>Linux Unleashed, Third Edition:Shell Programming</TITLE>
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!--ISBN=0672313723//-->
<!--TITLE=Linux Unleashed, Third Edition//-->
<!--AUTHOR=Tim Parker//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Sams//-->
<!--CHAPTER=14//-->
<!--PAGES=261-264//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="../ch13/257-260.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="264-266.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H2><A NAME="Heading1"></A><FONT COLOR="#000077">Chapter 14<BR>Shell Programming
</FONT></H2>
<P><I>by Rick McMullin</I></P>
<DL>
<DT><B>In This Chapter</B>
<DT>• Creating and running shell programs
<DT>• Using variables
<DT>• The importance of quotation marks
<DT>• The test command
<DT>• Conditional statements
<DT>• Iteration statements
<DT>• Functions
</DL>
<P>The last three chapters described how to use the most common Linux shell programs; now let’s take a closer look at the powerful interpretive programming languages that these shell programs have built into them.
</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:</P>
<DL>
<DD><B>•</B> Creating and running shell programs
<DD><B>•</B> Using shell variables
<DD><B>•</B> The importance of quotation marks
<DD><B>•</B> The <TT>test</TT> command
<DD><B>•</B> Conditional statements
<DD><B>•</B> Iteration statements
<DD><B>•</B> Functions
</DL>
<P>This chapter contains several small examples of shell programs. Each new concept or command that is introduced has some example code that further explains what is being presented.
</P>
<H3><A NAME="Heading2"></A><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 first create a file using a text editor and put the shell or Linux commands you want to be executed into that file. For example, we’ll 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:</P>
<!-- CODE SNIP //-->
<PRE>
umount /dev/cdrom
mount -t iso1960 /dev/cdrom /cdrom
</PRE>
<!-- END CODE SNIP //-->
<P>Instead of typing both of these commands each time you change the CD in your drive, you can create a shell program that will execute both of these commands for you. To do this, put the two commands into a file and name the file <TT>remount</TT> (or any other name you want).</P>
<P>Several ways of executing the commands are contained in the <TT>remount</TT> file. One way to accomplish this is to make the file executable. This is done by entering the following command:</P>
<!-- CODE SNIP //-->
<PRE>
chmod +x remount
</PRE>
<!-- END CODE SNIP //-->
<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 <B>remount</B> on the command line.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>Note: </B><BR>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></FONT>
</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 <TT>tcsh</TT> program, this is done by entering the following command:</P>
<!-- CODE SNIP //-->
<PRE>
tcsh remount
</PRE>
<!-- END CODE SNIP //-->
<P>This command starts up a new shell and tells it to execute the commands that are found in the <TT>remount</TT> 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:</P>
<!-- CODE SNIP //-->
<PRE>
. remount
</PRE>
<!-- END CODE SNIP //-->
<P>To do the same thing in <TT>tcsh</TT>, type the following command:</P>
<!-- CODE SNIP //-->
<PRE>
source remount
</PRE>
<!-- END CODE SNIP //-->
<P>Another situation in which a simple shell program can save a lot of time is described in the following example. Let’s say that you are working on three different files in a directory, and at the end of every day you want to back up those three files onto a floppy disk. To do this, simply type a series of commands similar to the following:
</P>
<!-- CODE SNIP //-->
<PRE>
mount -t msdos /dev/fd0 /a
cp file1 /a
cp file2 /a
cp file3 /a
</PRE>
<!-- END CODE SNIP //-->
<P>As stated in the example, one way of doing this is to mount the floppy drive and then type three copy commands, one for each file you want to copy. A simpler way is to put the four commands into a text file called <TT>backup</TT> and then execute the <TT>backup</TT> command when you want to copy the three files onto the floppy drive.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>Note: </B><BR>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></FONT>
</BLOCKQUOTE>
<H3><A NAME="Heading3"></A><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’ve already seen in the introductory shell chapters some of the ways in which shell variables can be used. Two of the variables 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.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="../ch13/257-260.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="264-266.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?