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

📄 lsg26.htm

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







while ( "$*" != "" )







 echo "This is parameter number $count $1"







 shift







 set count = `expr $count + 1`







end</FONT></PRE>







<BR>







<A NAME="E69E157"></A>







<H4 ALIGN=CENTER>







<CENTER>







<FONT SIZE=4 COLOR="#FF0000"><B>The until Statement</B></FONT></CENTER></H4>







<BR>







<P>The until statement is very similar in syntax and function to the while statement. The only real difference between the two is that the until statement executes its code block while its conditional expression is false and the while statement executes its code block while its conditional expression is true. The syntax for the until statement in bash and pdksh is the following:







<BR>







<PRE>







<FONT COLOR="#000080">until expression







do







 commands







done</FONT></PRE>







<P>To make the example that was used for the while statement work with the until statement, all you have to do is negate the condition, as shown in the following code:







<BR>







<PRE>







<FONT COLOR="#000080">count=1







until [ -z &quot;$*&quot; ]







do







 echo &quot;This is parameter number $count $1&quot;







 shift







 count=`expr $count + 1`







done</FONT></PRE>







<P>The only difference between this example and the while statement example is that the -n test command option, which means that the string has non-zero length, was replaced by the -z test option, which means that the sting has a length of zero. In practice, the until statement is not very useful because any until statement that you write can also be written as a while statement. The until command is not supported by tcsh.







<BR>







<BR>







<A NAME="E69E158"></A>







<H4 ALIGN=CENTER>







<CENTER>







<FONT SIZE=4 COLOR="#FF0000"><B>The shift Command</B></FONT></CENTER></H4>







<BR>







<P>The bash, pdksh, and tcsh shells all support a command called shift. The shift command moves the current values stored in the positional parameters one position to the left. For example, if the values of the current positional parameters are







<BR>







<BR>







<PRE>







<FONT COLOR="#000080">$1 = -r $2 = file1 $3 = file2</FONT></PRE>







<P>and you executed the shift command







<BR>







<BR>







<PRE>







<FONT COLOR="#000080">shift</FONT></PRE>







<P>the resulting positional parameters would be the following:







<BR>







<BR>







<PRE>







<FONT COLOR="#000080">$1 = file1 $2 = file2</FONT></PRE>







<P>You also can shift the positional parameters over more than one place by specifying a number with the shift command. The following command shifts the positional parameters two places:







<BR>







<BR>







<PRE>







<FONT COLOR="#000080">shift 2</FONT></PRE>







<P>This command is very useful when you have a shell program that needs to parse command line options. Options are typically preceded by a hyphen and a letter that indicates what the option is to be used for. Because options are usually processed in a loop of some kind, you will often want to skip to the next positional parameter once you have identified which option should be coming next. For example, the following shell program expects two command line options, one that specifies an input file and one that specifies an output file. The program reads the input file, translates all of the characters in the input file into uppercase, and then stores the results in the specified output file:







<BR>







<PRE>







<FONT COLOR="#000080">while [ &quot;$1&quot; ]







do







 if [ &quot;$1&quot; = &quot;-i&quot; ] then







 infile=&quot;$2&quot;







 shift 2







 else if [ &quot;$1&quot; = &quot;-o&quot; ] then







 outfile=&quot;$2&quot;







 shift 2







 else







 echo &quot;Program $0 does not recognize option $1&quot;







 fi







done







tr a-z A-Z &lt;$infile &gt;$outfile</FONT></PRE>







<BR>







<A NAME="E69E159"></A>







<H4 ALIGN=CENTER>







<CENTER>







<FONT SIZE=4 COLOR="#FF0000"><B>The select Statement</B></FONT></CENTER></H4>







<BR>







<P>The pdksh shell offers one iteration statement that neither bash nor tcsh provides. This statement is the very useful select. statement. It is quite a bit different from the other iteration statements because it does not execute a block of shell code repeatedly while a condition is true or false. What the select statement does is enable you to automatically generate simple text menus. The syntax for the select statement is as follows:







<BR>







<PRE>







<FONT COLOR="#000080">select menuitem [in list_of_items]







do







 commands







done</FONT></PRE>







<P>When you execute a select statement, pdksh creates a numbered menu item for each element that is in the list_of_items. This list_of_items can be a variable that contains more that one item such as choice1 choice2 or it can be a list of choices typed in the command, as in the following example:







<BR>







<BR>







<PRE>







<FONT COLOR="#000080">select menuitem in choice1 choice2 choice3</FONT></PRE>







<P>If the list_of_items is not provided, the select statement uses the positional parameters just as the for statement does.







<BR>







<P>When the user of the program that contains a select statement picks one of the menu items by typing in the number associated with it, the select statement stores the value of the selected item in the menuitem variable. The statements in the do block can then perform actions on this menu item.







<BR>







<P>The following is an example of how you can use the select statement. This example displays three menu items. When the user chooses an item, the program asks whether that item is the intended selection. If the user enters anything other than y or Y, the program redisplays the menu.







<BR>







<PRE>







<FONT COLOR="#000080">select menuitem in pick1 pick2 pick3







do







 echo &quot;Are you sure you want to pick $menuitem&quot;







 read res







 if [ $res = &quot;y&quot; -o $res = &quot;Y&quot; ]







 then







 break







 fi







done</FONT></PRE>







<P>This example introduces a few new commands. The read command is used to get input from the user. It stores anything that the user types into the specified variable. The break command is used to exit a while, select, or for statement.







<BR>







<BR>







<A NAME="E69E160"></A>







<H4 ALIGN=CENTER>







<CENTER>







<FONT SIZE=4 COLOR="#FF0000"><B>The repeat Statement</B></FONT></CENTER></H4>







<BR>







<P>The tcsh shell has an iteration statement that has no equivalent in the pdksh or bash shells. This statement is the repeat statement. The repeat statement executes a single command a specified number of times. The syntax for the repeat statement is the following:







<BR>







<BR>







<PRE>







<FONT COLOR="#000080">repeat count command</FONT></PRE>







<P>The following example of the repeat statement takes a set of numbers as command line options and prints out that number of periods onto the screen. This program acts as a very primitive graphing program.







<BR>







<PRE>







<FONT COLOR="#000080">#







foreach num ($*)







 repeat $num echo -n &quot;.&quot;







 echo &quot;&quot;







end</FONT></PRE>







<BLOCKQUOTE>







<BLOCKQUOTE>







<HR ALIGN=CENTER>







<BR>







<NOTE>You can rewrite any repeat statement as a while or for statement; the repeat syntax is just more convenient.</NOTE>







<BR>







<HR ALIGN=CENTER>







</BLOCKQUOTE></BLOCKQUOTE>







<BR>







<A NAME="E68E147"></A>







<H3 ALIGN=CENTER>







<CENTER>







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







<BR>







<P>The shell languages enable you to define your own functions. These functions behave in much the same way as functions that you define in C or other programming languages. The main advantage of using functions as opposed to writing all of your shell code in line is for organization. Code written using functions tends to be much easier to read and maintain and also tends to be smaller because you can group common code into functions instead of putting it everywhere that it is needed.







<BR>







<P>The syntax for creating a function in bash and pdksh is the following:







<BR>







<PRE>







<FONT COLOR="#000080">fname () {







 shell commands







}</FONT></PRE>







<P>In addition to the preceding syntax, pdksh allows the following syntax:







<BR>







<PRE>







<FONT COLOR="#000080">function fname {







 shell commands







}</FONT></PRE>







<P>Both of these forms behave in the exact same way.







<BR>







<P>After you have defined your function using one of the preceding forms, you can invoke it by entering the following command:







<BR>







<BR>







<PRE>







<FONT COLOR="#000080">fname [parm1 parm2 parm3 ...]</FONT></PRE>







<P>Notice that you can pass any number of parameters to your function. When you do pass parameters to a function, it sees those parameters as positional parameters just as a shell program does when you pass it parameters on the command line. For example, the following shell program contains several functions each of which is performing a task that is associated with one of the command line options. This example illustrates many of the concepts covered in this chapter. It reads all the files that are passed in on the command line and, depending on the option that was used, writes the files out in all uppercase letters, writes the files out in all lowercase letters, or prints the files.







<BR>







<PRE>







<FONT COLOR="#000080">upper () {







 shift







 for i







 do







 tr a-z A-Z &lt;$1 &gt;$1.out







 rm $1







 mv $1.out $1







 shift







 done; }







lower () {







 shift







 for i







 do







 tr A-Z a-z &lt;$1 &gt;$1.out







 rm $1







 mv $1.out $1







 shift







 done; }







print () {







 shift







 for i







 do







 lpr $1







 shift







 done; }







usage_error () {







 echo &quot;$1 syntax is $1 &lt;option&gt; &lt;input files&gt;&quot;







 echo &quot;&quot;







 echo &quot;where option is one of the following&quot;







 echo &quot;p -- to print frame files&quot;







 echo &quot;u -- to save as uppercase&quot;







 echo &quot;l -- to save as lowercase&quot;; }







case $1







in







 p | -p) print $@;;







 u | -u) upper $@;;







 l | -l) lower $@;;







 *) usage_error $0;;







esac</FONT></PRE>







<P>The tcsh program does not support functions.







<BR>







<BR>







<A NAME="E68E148"></A>







<H3 ALIGN=CENTER>







<CENTER>







<FONT SIZE=5 COLOR="#FF0000"><B>Summary</B></FONT></CENTER></H3>







<BR>







<P>In this chapter, you have seen many of the features of the bash, pdksh and tcsh programming languages. As you become used to using Linux, you will find that you use shell programming languages more and more often. Even though the shell languages are very powerful and quite easy to learn, you may run into some situations where shell programs are not suited to the problem you are solving. In these cases, you may want to investigate the possibility of using one of the other languages that is available under Linux.







<P ALIGN=LEFT>























































</td>
</tr>
</table>

<!-- begin footer information -->



</body></html>

⌨️ 快捷键说明

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