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

📄 ch13.htm

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTM
📖 第 1 页 / 共 5 页
字号:
	<DT><FONT COLOR="#0066FF"></FONT></DT>



</DL>











<DL>



	<DD>



<HR>



<A NAME="Heading10<FONT COLOR="#000077"><B>NOTE: </B></FONT>If you omitted



	the <TT>$</TT> from the preceding command, the <TT>echo</TT> command would display



	the word <TT>count</TT> on-screen.



<HR>







</DL>







<H4 ALIGN="CENTER"><A NAME="Heading11<FONT COLOR="#000077">Positional Parameters



and Other Built-In Shell Variables</FONT></H4>



<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 <TT>1</TT>, the second parameter



is stored into a variable named <TT>2</TT>, 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 (<TT>$</TT>) just as you do with variables you define.</P>



<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.<FONT



COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">#program reverse, prints the command line parameters out in reverse #order



</FONT></PRE>







<PRE><FONT COLOR="#0066FF">echo &quot;$2&quot; &quot;$1&quot;



</FONT></PRE>



<P>If you invoked this program by entering<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">reverse hello there



</FONT></PRE>



<P>the program would return the following output:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">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>







<CENTER>



<P><FONT SIZE="4"><B>Table 13.1. Built-in shell variables. </B></FONT>



<TABLE BORDER="0">



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><I>Variable </I></TD>



		<TD ALIGN="LEFT"><I>Use </I></TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>$#</TT> </TD>



		<TD ALIGN="LEFT">Stores the number of command-line arguments that were passed to the shell program.



					</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>$?</TT> </TD>



		<TD ALIGN="LEFT">Stores the exit value of the last command that was executed. </TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>$0</TT> </TD>



		<TD ALIGN="LEFT">Stores the first word of the entered command (the name of the shell program). </TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>$*</TT> </TD>



		<TD ALIGN="LEFT">Stores all the arguments that were entered on the command line (<TT>$1 $2 ...</TT>).



					</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>&quot;$@&quot;</TT> </TD>



		<TD ALIGN="LEFT">Stores all the arguments that were entered on the command line, individually quoted



			(<TT>&quot;$1&quot; &quot;$2&quot; ...</TT>). </TD>



	</TR>



</TABLE>







</CENTER>



<CENTER>



<H3><A NAME="Heading12<FONT COLOR="#000077">The Importance of Quotation Marks</FONT></H3>



</CENTER>



<P>The use of the different types of quotation marks is very important in shell programming.



All three kinds of quotation marks and the backslash character are used by the shell



to perform different functions. The double quotation marks (<TT>&quot;&quot;</TT>),



the single quotation marks (<TT>`'</TT>), and the backslash (<TT>\</TT>) are all



used to hide special characters from the shell. Each of these methods hides varying



degrees of special characters from the shell.</P>



<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 <TT>hello



there</TT> to the variable <TT>greeting</TT>, you would type the following command:<FONT



COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">greeting=&quot;hello there&quot; (for bash and pdksh)



set greeting = &quot;hello there&quot; (for tcsh)



</FONT></PRE>



<P>This command would store the <TT>hello there</TT> string into the <TT>greeting</TT>



variable as one word. If you typed this command without using the quotes, you would



not get the results you wanted. <TT>bash</TT> and <TT>pdksh</TT> would not understand



the command and would return an error message. <TT>tcsh</TT> would assign the value



<TT>hello</TT> to the <TT>greeting</TT> variable and ignore the rest of the command



line.</P>



<P>Single quotes are the most powerful form of quoting. They hide all special characters



from the shell. This is useful if the command that you enter is intended for a program



other than the shell.</P>



<P>Because the single quotes are the most powerful, you could have written the <TT>hello



there</TT> variable assignment using single quotes. You might not always want to



do this. If the string being assigned to the <TT>greeting</TT> variable contained



another variable, you would have to use the double quotes. For example, if you wanted



to include the name of the user in your greeting, you would type the following command:<FONT



COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">greeting=&quot;hello there $LOGNAME&quot; (for bash and pdksh)



set greeting=&quot;hello there $LOGNAME&quot; (for tcsh)



</FONT></PRE>







<DL>



	<DT><FONT COLOR="#0066FF"></FONT></DT>



</DL>











<DL>



	<DD>



<HR>



<A NAME="Heading13<FONT COLOR="#000077"><B>NOTE: </B></FONT>Remember that the



	<TT>LOGNAME</TT> variable is a shell variable that contains the Linux username of



	the person who is logged in to the system.



<HR>







</DL>







<P>This would store the value <TT>hello there root</TT> into the <TT>greeting</TT>



variable if you were logged in to Linux as root. If you tried to write this command



using single quotes it wouldn't work, because the single quotes would hide the dollar



sign from the shell and the shell wouldn't know that it was supposed to perform a



variable substitution. The <TT>greeting</TT> variable would be assigned the value



<TT>hello there $LOGNAME</TT> if you wrote the command using single quotes.</P>







<P>Using the backslash is the third way of hiding special characters from the shell.



Like the single quotation mark method, the backslash hides all special characters



from the shell, but it can hide only one character at a time, as opposed to groups



of characters. You could rewrite the greeting example using the backslash instead



of double quotation marks by using the following command:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">greeting=hello\ there (for bash and pdksh)



</FONT></PRE>







<PRE><FONT COLOR="#0066FF">set greeting=hello\ there (for tcsh)



</FONT></PRE>



<P>In this command, the backslash hides the space character from the shell, and the



string <TT>hello there</TT> is assigned to the <TT>greeting</TT> variable.</P>



<P>Backslash quoting is used most often when you want to hide only a single character



from the shell. This is usually done when you want to include a special character



in a string. For example, if you wanted to store the price of a box of computer disks



into a variable named <TT>disk_price</TT>, you would use the following command:<FONT



COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">disk_price=\$5.00 (for bash and pdksh)







set disk_price = \$5.00 (for tcsh)



</FONT></PRE>



<P>The backslash in this example would hide the dollar sign from the shell. If the



backslash were not there, the shell would try to find a variable named <TT>5</TT>



and perform a variable substitution on that variable. Assuming that no variable named



<TT>5</TT> were defined, the shell would assign a value of <TT>.00</TT> to the <TT>disk_price</TT>



variable. This is because the shell would substitute a value of null for the <TT>$5</TT>



variable.







<DL>



	<DT></DT>



</DL>











<DL>



	<DD>



<HR>



<A NAME="Heading14<FONT COLOR="#000077"><B>NOTE: </B></FONT>The <TT>disk_price</TT>



	example could also have used single quotes to hide the dollar sign from the shell.



<HR>







</DL>







<P>The back quote marks (<TT>''</TT>) perform a different function. They are used



when you want to use the results of a command in another command. For example, if



you wanted to set the value of the variable <TT>contents</TT> equal to the list of



files in the current directory, you would type the following command:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">contents='ls' (for bash and pdksh)







set contents = 'ls' (for tcsh)



</FONT></PRE>



<P>This command would execute the <TT>ls</TT> command and store the results of the



command into the <TT>contents</TT> variable. As you will see in the section &quot;Iteration



Statements,&quot; this feature can be very useful when you want to write a shell



program that performs some action on the results of another command.



<CENTER>



<H3><A NAME="Heading15<FONT COLOR="#000077">The test Command</FONT></H3>



</CENTER>



<P>In <TT>bash</TT> and <TT>pdksh</TT>, a command called <TT>test</TT> is used to



evaluate conditional expressions. You would typically use the <TT>test</TT> command



to evaluate a condition that is used in a conditional statement or to evaluate the



entrance or exit criteria for an iteration statement. The <TT>test</TT> command has



the following syntax:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">test expression



</FONT></PRE>



<P>or<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">[ expression ]



</FONT></PRE>



<P>Several built-in operators can be used with the <TT>test</TT> command. These operators



can be classified into four groups: integer operators, string operators, file operators,



and logical operators.</P>







<P>The shell integer operators perform similar functions to the string operators



except that they act on integer arguments. Table 13.2 lists the <TT>test</TT> command's



integer operators. <BR>







<CENTER>



<P><FONT SIZE="4"><B>Table 13.2. The test command's integer operators. </B></FONT>



<TABLE BORDER="0">



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><I>Operator </I></TD>



		<TD ALIGN="LEFT"><I>Meaning </I></TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>int1 -eq int2</TT> </TD>



		<TD ALIGN="LEFT">Returns True if <TT>int1</TT> is equal to <TT>int2</TT>. </TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>int1 -ge int2</TT> </TD>



		<TD ALIGN="LEFT">Returns True if <TT>int1</TT> is greater than or equal to <TT>int2</TT>. </TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>int1 -gt int2</TT> </TD>



		<TD ALIGN="LEFT">Returns True if <TT>int1</TT> is greater than <TT>int2</TT>. </TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>int1 -le int2</TT> </TD>



		<TD ALIGN="LEFT">Returns True if <TT>int1</TT> is less than or equal to <TT>int2</TT>. </TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>int1 -lt int2</TT> </TD>



		<TD ALIGN="LEFT">Returns True if <TT>int1</TT> is less than <TT>int2</TT>. </TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>int1 -ne int2</TT> </TD>



		<TD ALIGN="LEFT">Returns True if <TT>int1</TT> is not equal to <TT>int2</TT>. </TD>



	</TR>



</TABLE>



<BR>







</CENTER>



<P>The string operators are used to evaluate string expressions. Table 13.3 lists



the string operators that are supported by the three shell programming languages.







<CENTER>



<P><BR>



<FONT SIZE="4"><B>Table 13.3. The test command's string operators. </B></FONT>



<TABLE BORDER="0">



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><I>Operator </I></TD>



		<TD ALIGN="LEFT"><I>Meaning </I></TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>str1 = str2</TT> </TD>



		<TD ALIGN="LEFT">Returns True if <TT>str1</TT> is identical to <TT>str2</TT>. </TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>str1 != str2</TT> </TD>



		<TD ALIGN="LEFT">Returns True if <TT>str1</TT> is not identical to <TT>str2</TT>. </TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>str</TT> </TD>



		<TD ALIGN="LEFT">Returns True if <TT>str</TT> is not null. </TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>-n str</TT> </TD>



		<TD ALIGN="LEFT">Returns True if the length of <TT>str</TT> is greater than zero. </TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>-z str</TT> </TD>



		<TD ALIGN="LEFT">Returns True if the length of <TT>str</TT> is equal to zero. </TD>



	</TR>



</TABLE>



<BR>







</CENTER>



<P>The <TT>test</TT> command's file operators are used to perform functions such



as checking to see if a file exists and checking to see what kind of file is passed



as an argument to the <TT>test</TT> command. Table 13.4 lists the <TT>test</TT> command's



file operators. <BR>







<CENTER>



<P><FONT SIZE="4"><B>Table 13.4. The test command's file operators. </B></FONT>



<TABLE BORDER="0">

⌨️ 快捷键说明

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