📄 lsg26.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>
-->
<LINK REL="ToC" HREF="index.htm">
<LINK REL="Index" HREF="htindex.htm">
<LINK REL="Next" HREF="lsgpt04.htm">
<A NAME="I0"></A>
<H2>Linux System Administrator's Survival Guide lsg26.htm</H2>
<P ALIGN=LEFT>
<HR ALIGN=CENTER>
<P>
<UL>
<UL>
<UL>
<LI>
<A HREF="#E68E141" >Creating and Running Shell Programs</A>
<LI>
<A HREF="#E68E142" >Using Variables</A>
<UL>
<LI>
<A HREF="#E69E151" >Assigning a Value to a Variable</A>
<LI>
<A HREF="#E69E152" >Understanding Positional Parameters and Other Built-In Shell Variables</A></UL>
<LI>
<A HREF="#E68E143" >Using Quotation Marks</A>
<LI>
<A HREF="#E68E144" >Using the test Command</A>
<LI>
<A HREF="#E68E145" >Using Conditional Statements</A>
<UL>
<LI>
<A HREF="#E69E153" >The if Statement</A>
<LI>
<A HREF="#E69E154" >The case Statement</A></UL>
<LI>
<A HREF="#E68E146" >Using Iteration Statements</A>
<UL>
<LI>
<A HREF="#E69E155" >The for Statement</A>
<LI>
<A HREF="#E69E156" >The while Statement</A>
<LI>
<A HREF="#E69E157" >The until Statement</A>
<LI>
<A HREF="#E69E158" >The shift Command</A>
<LI>
<A HREF="#E69E159" >The select Statement</A>
<LI>
<A HREF="#E69E160" >The repeat Statement</A></UL>
<LI>
<A HREF="#E68E147" >Using Functions</A>
<LI>
<A HREF="#E68E148" >Summary</A></UL></UL></UL>
<HR ALIGN=CENTER>
<A NAME="E66E29"></A>
<H1 ALIGN=CENTER>
<CENTER>
<FONT SIZE=6 COLOR="#FF0000"><B>Chapter 26</B></FONT></CENTER></H1>
<BR>
<A NAME="E67E32"></A>
<H2 ALIGN=CENTER>
<CENTER>
<FONT SIZE=6 COLOR="#FF0000"><B>Shell Programming</B></FONT></CENTER></H2>
<BR>
<P>Shell programming is one of the most useful tools a system administrator has. The ability to write a short program to complete an otherwise time-consuming task is much more powerful than knowing every Linux administration tool in detail. Shell programming can make a system administrator's life so much easier that it should be a mandatory skill.
<BR>
<P>If these statements sound a little too good to be true, consider the many tasks system administrators face every day involving multiple files or directories. Whenever you deal with a number of files, shell programming can make your job easier. This chapter can't show you much more than the basics, but they should help you in your daily work. Lots of excellent books on shell programming are available, so be sure to keep one handy.
<BR>
<BR>
<A NAME="E68E141"></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. You can use these programs 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 that you want to be executed into that file. Suppose that 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 change the CD that is in the drive at a later time, you must force Linux to read the new directory contents. One way of achieving this task is to put the new CD into the drive, unmount the CD-ROM drive using the umount command, and then remount the drive using the mount command. The following commands show this sequence of steps:
<BR>
<PRE>
<FONT COLOR="#000080">umount /dev/cdrom
mount /dev/cdrom /cdrom</FONT></PRE>
<P>Instead of typing both of these commands each time you change the CD, you can create a shell program that executes both of these commands for you. To create this shell program, put the two commands into a file and call the file remount (or any other name you want).
<BR>
<P>There are several ways of executing the commands in the remount file. One way is to make the file executable 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 executable. To run your new shell program, type 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. If you can't run the command because it isn't found, specify the path.
<BR>Also, if you are using tcsh to write programs, the first line of the shell program must start with a # in order for tcsh to recognize it as a tcsh program file. In fact, it is safest to make sure the first line of every shell program is #!/bin/sh to make sure the shell program is executed as a Bourne shell process. This prevents many problems with the C shell trying to interpret Bourne shell syntax.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>Another way that you can execute the shell program is to run the shell that the program was written for and pass the program as a parameter to the shell. In the case of a tcsh program, you enter 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 in the remount file.
<BR>
<P>A third way of executing the commands that are in a shell program file is to use the . (dot) command (with both the pdksh and bash shells) or the source command in the tcsh shell. These commands tell the shell to execute the file that is passed as an argument. For example, you can use the following command 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, use the following command:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">source remount</FONT></PRE>
<P>The following example shows another situation in which a simple shell program can save a lot of time. Suppose that you were working on three different files in a directory every day, and you wanted to back up those three files onto a floppy disk at the end of each day. To do this task, you type in a series of commands:
<BR>
<PRE>
<FONT COLOR="#000080">mount -t msdos /dev/fd0 /a
cp file1 /dev/fd0
cp file2 /dev/fd0
cp file3 /dev/fd0</FONT></PRE>
<P>One way of backing up the files is to mount the floppy disk and then type three copy commands, one for each file that you want to copy. A simpler way is to put the four commands into a file called backup and then execute the backup command when you want to copy the three files onto the floppy disk drive.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>You still have to ensure the backup shell program is executable and is in a directory that is in your path before you run the command. Also, be careful about using a filename that corresponds to a command name. If there is a program called backup, for example, in the shell's search path before it reads the current directory, that command would execute instead of the shell command file. For this reason, try to use filenames for your shell scripts that are not close to Linux commands.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E68E142"></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 have seen several types of variables before, of course. Some examples of commonly used variables are the PATH variable and the TERM variable. These variables are examples of built-in shell variables, which are variables that are defined by the shell program that you are using. This section describes how you can create your own variables and use them in simple shell programs.
<BR>
<BR>
<A NAME="E69E151"></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 supplied with Linux (Bourne, Korn, and C shell variants), you can assign a value to a variable by typing the variable name followed by an equal sign and then typing the value that you want to assign to the variable. For example, to assign a value of five to the variable named count, enter the following command in bash or pdksh:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">count=5</FONT></PRE>
<P>With tcsh, 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>When setting a variable for the bash and pdksh shells, make sure that there are no spaces on either side of the equal sign. With tcsh, spaces do not matter.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>Because the shell language is a non-typed interpretive language, you do not have to declare the variable as you would if you were programming in C or Pascal. You can use the same variable to store character strings or integers. You store a character string into a variable in the same way you store an integer into a variable, as shown in the following example:
<BR>
<PRE>
<FONT COLOR="#000080">name=Garry (for pdksh and bash)
set name = Garry (for tcsh)</FONT></PRE>
<P>After you store a value into a variable, how do you get the value back out? You precede the variable name with a dollar sign ($).To print the value that stored in the count variable to the screen, enter the following command:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">echo $count</FONT></PRE>
<P>If you omit the $ from the preceding command, the echo command displays the word count on-screen.
<BR>
<BR>
<A NAME="E69E152"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Understanding Positional Parameters and Other Built-In Shell Variables</B></FONT></CENTER></H4>
<BR>
<P>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 on. The shell reserves these variable names so you cannot use them as variables that you define. To access the values that are stored in these variables, you must precede the variable name with a dollar sign ($) just as you do with variables that 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"
echo "$1"</FONT></PRE>
<P>If you invoked this program by entering the command
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">reverse hello there</FONT></PRE>
<P>the program would return the output
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">there hello</FONT></PRE>
<P>A number of other built-in shell variables are important to know about when you are doing a lot of shell programming. Table 26.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 26.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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -