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

📄 lsg26.htm

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTM
📖 第 1 页 / 共 5 页
字号:


<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 executed command</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, which is 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 (&quot;$1 $2 ...&quot;)</FONT>







<TR>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







&quot;$@&quot;







</FONT>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







Stores all arguments that were entered on the command line, individually quoted (&quot;$1&quot; &quot;$2&quot; ...)</FONT>







</TABLE><BR>







<A NAME="E68E143"></A>







<H3 ALIGN=CENTER>







<CENTER>







<FONT SIZE=5 COLOR="#FF0000"><B>Using Quotation Marks</B></FONT></CENTER></H3>







<BR>







<P>The use of the different types of quotation marks is very important in shell programming. The shell uses both kinds of quotation marks and the backslash character to perform different functions. The double quotation marks (&quot;&quot;), the single quotation marks (''), and the backslash (\) are all used to hide special characters from the shell. The back quotes have a special meaning for the shell and should not be used to enclose strings. Each of these methods hide 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. This type of quoting is most useful when you are assigning strings that contain more than one word to a variable. For example, to assign the string hello there to the variable called greeting, enter the following commands:







<BR>







<PRE>







<FONT COLOR="#000080">greeting=&quot;hello there&quot; (in bash and pdksh)







set greeting = &quot;hello there&quot; (in tcsh)</FONT></PRE>







<P>This command stores the whole hello there string into the greeting variable as one word. If you typed in this command without using the quotes, bash and pdksh wouldn't understand the command and would return an error message, and tcsh would assign the value hello to the greeting variable and ignore the rest of the command line.







<BR>







<P>Single quotes are the most powerful form of quoting. They hide all special characters from the shell. This type of quoting is useful if the command you enter is intended for a program other than the shell. You can, for example, use single quotes to write the hello there variable assignment, but you can't use this method in some instances. If the string being assigned to the greeting variable contains another variable, for example, you have to use double quotes. Suppose you wanted to include the name of the user in your greeting. You would type the following commands:







<BR>







<PRE>







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







set greeting=&quot;hello there $LOGNAME&quot; (for tcsh)</FONT></PRE>







<BLOCKQUOTE>







<BLOCKQUOTE>







<HR ALIGN=CENTER>







<BR>







<NOTE>The LOGNAME variable is a shell variable that contains the Linux username of the person that is logged on to the system.</NOTE>







<BR>







<HR ALIGN=CENTER>







</BLOCKQUOTE></BLOCKQUOTE>







<P>These commands stores the value hello there root into the greeting variable if you are logged into Linux as root. If you try to write this command using single quotes, the single quotes hide the dollar sign from the shell, and the shell doesn't know that it is supposed to perform a variable substitution. As a result, the greeting variable is assigned the value of hello there $LOGNAME.







<BR>







<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 can rewrite the greeting example using the backslash instead of double quotation marks by using the following commands:







<BR>







<PRE>







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







set greeting=hello\ there (for tcsh)</FONT></PRE>







<P>In this command, the backslash hides the space character from the shell and the string hello there is assigned to the greeting variable.







<BR>







<P>Backslash quoting is used most often when you want to hide only a single character from the shell. This situation occurs when you want to include a special character in a string. For example, to store the price of a box of computer disks into a variable named disk_price, use the following command.







<BR>







<PRE>







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







set disk_price = \$5.00 (tcsh)</FONT></PRE>







<P>The backslash in this example hides the dollar sign from the shell. If the backslash were not there, the shell would try to find a variable named 5 and perform a variable substitution on that variable. If there were no variables named 5 defined, the shell would assign a value of .00 to the disk_price variable. (This shell would substitute a value of null for the $5 variable.) You could also use single quotes in the disk_price example to hide the dollar sign from the shell.







<BR>







<P>The back quote marks (``) perform a different function. You use them when you want to use the results of a command in another command. For example, to set the value of the contents variable to be equal to the list of files that are in the current directory, type the following command:







<BR>







<PRE>







<FONT COLOR="#000080">contents=`ls` (for bash and pdksh)







set contents = `ls` (for tcsh)</FONT></PRE>







<P>This command executes the ls command and stores the results of the command into the contents variable. As shown later in the iteration statements section, this feature can be very useful when you want to write a shell program that performs some action on the results of a another command.







<BR>







<BR>







<A NAME="E68E144"></A>







<H3 ALIGN=CENTER>







<CENTER>







<FONT SIZE=5 COLOR="#FF0000"><B>Using the test Command</B></FONT></CENTER></H3>







<BR>







<P>In bash and pdksh, the test command is used to evaluate conditional expressions. You typically use the test command to evaluate a condition in a conditional statement or to evaluate the entrance or exit criteria for an iteration statement. The test command has the following syntax:







<BR>







<BR>







<PRE>







<FONT COLOR="#000080">test expression</FONT></PRE>







<P>or







<BR>







<BR>







<PRE>







<FONT COLOR="#000080">[ expression ]</FONT></PRE>







<P>You can use several built-in operators with the test command. These operators are classified into four different groups: string operators, integer operators, file operators, and logical operators.







<BR>







<P>You use the string operators to evaluate string expressions. Table 26.2 lists the string operators that the three shell programming languages support.







<BR>







<BR>







<P ALIGN=CENTER>







<CENTER>







<FONT COLOR="#000080"><B>Table 26.2. String operators for the </B><B>test</B><B> command.</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>Operator</I>







</FONT>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







<I>Meaning</I></FONT>







<TR>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







str1 = str2







</FONT>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







Returns true if str1 is identical to str2</FONT>







<TR>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







str1 != str2







</FONT>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







Returns true if str1 is not identical to str2</FONT>







<TR>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







str







</FONT>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







Returns true if str is not null</FONT>







<TR>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







-n str







</FONT>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







Returns true if the length of str is greater than zero</FONT>







<TR>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







-z str







</FONT>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







Returns true if the length of str is equal to zero</FONT>







</TABLE><P>The shell integer operators perform similar functions to the string operators except that they act on integer arguments. Table 26.3 lists the test command's integer operators.







<BR>







<BR>







<P ALIGN=CENTER>







<CENTER>







<FONT COLOR="#000080"><B>Table 26.3. Integer operators for the </B><B>test</B><B> command.</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>Operator</I>







</FONT>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







<I>Meaning</I></FONT>







<TR>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







int1 -eq int2







</FONT>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







Returns true if int1 is equal to int2</FONT>







<TR>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







int1 -ge int2







</FONT>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







Returns true if int1 is greater than or equal to int2</FONT>







<TR>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







int1 -gt int2







</FONT>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







Returns true if int1 is greater than int2</FONT>







<TR>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







int1 -le int2







</FONT>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







Returns true if int1 is less than or equal to int2</FONT>







<TR>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







int1 -lt int2







</FONT>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







Returns true if int1 is less than int2</FONT>







<TR>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







int1 -ne int2







</FONT>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







Returns true if int1 is not equal to int2</FONT>







</TABLE><P>You use the test command's file operators to perform functions such as checking to see whether a file exists and checking to see what kind of file the file passed as an argument to the test command is. Table 26.4 lists the test command's file operators.







<BR>







<BR>







<P ALIGN=CENTER>







<CENTER>







<FONT COLOR="#000080"><B>Table 26.4. File operators for the </B><B>test</B><B> command.</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>Operator</I>







</FONT>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







<I>Meaning</I></FONT>







<TR>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







-d file







</FONT>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







Returns true if the specified file is a directory</FONT>







<TR>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







-f file







</FONT>







<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>







Returns true if the specified file is an ordinary file</FONT>







<TR>



⌨️ 快捷键说明

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