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

📄 ch13.htm

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


is that the <TT>-n</TT> test command option (which means that the string has nonzero



length) was removed, and the <TT>-z</TT> test option (which means that the string



has zero length) was put in its place.</P>



<P>In practice the <TT>until</TT> statement is not very useful, because any <TT>until</TT>



statement you write can also be written as a <TT>while</TT> statement.







<DL>



	<DT></DT>



</DL>











<DL>



	<DD>



<HR>



<A NAME="Heading28<FONT COLOR="#000077"><B>NOTE:</B> </FONT><TT>tcsh</TT> does



	not have an equivalent of the <TT>until</TT> statement other than rewriting it as



	a <TT>while</TT> loop.



<HR>







</DL>







<CENTER>



<H4><A NAME="Heading29<FONT COLOR="#000077">The shift Command</FONT></H4>



</CENTER>



<P><TT>bash</TT>, <TT>pdksh</TT>, and <TT>tcsh</TT> all support a command called



<TT>shift</TT>. The <TT>shift</TT> command moves the current values stored in the



positional parameters to the left one position. For example, if the values of the



current positional parameters are<FONT COLOR="#0066FF"></FONT>



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



</FONT></PRE>



<P>and you executed the <TT>shift</TT> command<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">shift



</FONT></PRE>



<P>the resulting positional parameters would be as shown here:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">$1 = file1  $2 = file2



</FONT></PRE>



<P>You can also move the positional parameters over more than one place by specifying



a number with the <TT>shift</TT> command. The following command would shift the positional



parameters two places:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">shift 2



</FONT></PRE>



<P>This is a very useful command when you have a shell program that needs to parse



command-line options. This is true because 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 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 the characters in the input file into uppercase, then stores



the results in the specified output file.







<DL>



	<DT></DT>



</DL>











<DL>



	<DD>



<HR>



<A NAME="Heading30<FONT COLOR="#000077"><B>NOTE: </B></FONT>The following example



	was written using <TT>bash</TT>, <TT>pdksh</TT> syntax.



</DL>











<DL>



	<DD><FONT COLOR="#0066FF">while [ &quot;$1&quot; ]<BR>



	do<BR>



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



	            infile=&quot;$2&quot;<BR>



	            shift 2<BR>



	      elif [ &quot;$1&quot; = &quot;-o&quot; ]<BR>



	      then<BR>



	            outfile=&quot;$2&quot;<BR>



	            shift 2<BR>



	      else<BR>



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



	      fi<BR>



	done<BR>



	<BR>



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



<HR>



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



</DL>







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



<CENTER>



<H4><A NAME="Heading31<FONT COLOR="#000077">The select Statement</FONT></H4>



</CENTER>



<P><TT>pdksh</TT> offers one iteration statement that neither <TT>bash</TT> nor <TT>tcsh</TT>



provides. This is the <TT>select</TT> statement. This is actually a very useful statement.



It is quite a bit different from the other iteration statements because it actually



does not execute a block of shell code repeatedly while a condition is true or false.



What the <TT>select</TT> statement does is enable you to automatically generate simple



text menus. The syntax for the <TT>select</TT> statement is<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">select menuitem [in list_of_items]



do



      commands







done



</FONT></PRE>



<P>where square brackets are used to enclose the optional part of the statement.</P>



<P>When a <TT>select</TT> statement is executed, <TT>pdksh</TT> creates a numbered



menu item for each element in the list_of_items. This list_of_items can be a variable



that contains more than one item, such as <TT>choice1 choice2</TT>, or it can be



a list of choices typed in the command. For example:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">select menuitem in choice1 choice2 choice3



</FONT></PRE>



<P>If the <TT>list_of_items</TT> is not provided, the <TT>select</TT> statement uses



the positional parameters just as with the <TT>for</TT> statement.</P>



<P>Once the user of the program containing a <TT>select</TT> statement picks one



of the menu items by typing the number associated with it, the <TT>select</TT> statement



stores the value of the selected item in the <TT>menuitem</TT> variable. The statements



contained in the <TT>do</TT> block can then perform actions on this menu item.</P>



<P>The following example illustrates a potential use for the <TT>select</TT> statement.



This example displays three menu items, and when the user chooses one of them it



asks whether that was the intended selection. If the user enters anything other than



<TT>y</TT> or <TT>Y</TT>, the menu is redisplayed.<FONT COLOR="#0066FF"></FONT>



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



</FONT></PRE>







<PRE><FONT COLOR="#0066FF">done



</FONT></PRE>



<P>A few new commands are introduced in this example. The <TT>read</TT> command is



used to get input from the user. It stores anything that the user types into the



specified variable. The <TT>break</TT> command is used to exit a <TT>while</TT>,



<TT>until</TT>, <TT>repeat</TT>, <TT>select</TT>, or <TT>for</TT> statement.



<CENTER>



<H4><A NAME="Heading32<FONT COLOR="#000077">The repeat Statement</FONT></H4>



</CENTER>



<P><TT>tcsh</TT> has an iteration statement that has no equivalent in <TT>pdksh</TT>



or <TT>bash</TT>. This is the <TT>repeat</TT> statement. The <TT>repeat</TT> statement



executes a single command a specified number of times. The syntax for the <TT>repeat</TT>



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



<PRE><FONT COLOR="#0066FF">repeat count command



</FONT></PRE>



<P>The following is an example of the <TT>repeat</TT> statement. It takes a set of



numbers as command-line options and prints that number of periods to the screen.



This program acts as a very primitive graphing program.<FONT COLOR="#0066FF"></FONT>



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



foreach num ($*)



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



      echo &quot;&quot;







end



</FONT></PRE>







<DL>



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



</DL>











<DL>



	<DD>



<HR>



<A NAME="Heading33<FONT COLOR="#000077"><B>NOTE: </B></FONT>Any <TT>repeat</TT>



	statement can be rewritten as a <TT>while</TT> or <TT>for</TT> statement. The <TT>repeat</TT>



	syntax is simply more convenient.



<HR>







</DL>







<CENTER>



<H3><A NAME="Heading34<FONT COLOR="#000077">Functions</FONT></H3>



</CENTER>



<P>The shell languages enable you to define your own functions. These functions behave



in much the same way as functions 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 organizational purposes. 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 it is needed.</P>



<P>The syntax for creating a function in <TT>bash</TT> and <TT>pdksh</TT> is the



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



<PRE><FONT COLOR="#0066FF">fname () {



      shell commands







}



</FONT></PRE>



<P><TT>pdksh</TT> also allows the following syntax:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">function fname {



      shell commands



</FONT></PRE>







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



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



<P>Once you have defined your function using one of these forms, you can invoke it



by entering the following command:</P>



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



fname [parm1 parm2 parm3 ...]



</FONT></PRE>







<DL>



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



</DL>











<DL>



	<DD>



<HR>



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



	shell does not support functions.



<HR>







</DL>







<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 associated with one of the command-line options. This example illustrates



many of the topics covered in this chapter. It reads all the files that are passed



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



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



</FONT></PRE>







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



esac



</FONT></PRE>



<CENTER>



<H3><A NAME="Heading36<FONT COLOR="#000077">Summary</FONT></H3>



</CENTER>



<P>This chapter introduced you to many of the features of the <TT>bash</TT>, <TT>pdksh</TT>,



and <TT>tcsh</TT> programming languages. As you become familiar with using Linux,



you will find that you use shell programming languages more and more often.</P>







<P>Even though the shell languages are very powerful and also quite easy to learn,



you might 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 available under Linux. Some of your options are C and



C++, which are described in Chapters 27, &quot;Programming in C,&quot; and 28, &quot;Programming



in C++&quot;; <TT>gawk</TT>, which is described in Chapter 26, &quot;<TT>gawk</TT>&quot;;



and Perl, which is described in Chapter 29, &quot;Perl.&quot;



















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

<!-- begin footer information -->



</body></html>

⌨️ 快捷键说明

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