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

📄 ch13.htm

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




fi



</FONT></PRE>



<P>The same statement written using the <TT>tcsh</TT> syntax is shown here:<FONT



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



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



if ( { -f .profile } ) then



      echo &quot;There is a .profile file in the current directory.&quot;



else



      echo &quot;Could not find the .profile file.&quot;



</FONT></PRE>







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



endif



</FONT></PRE>







<DL>



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



</DL>











<DL>



	<DD>



<HR>



<A NAME="Heading21<FONT COLOR="#000077"><B>NOTE:</B> </FONT>Notice that in



	the <TT>tcsh</TT> example the first line starts with a <TT>#</TT>. This is required



	for <TT>tcsh</TT> to recognize the file containing the commands as a <TT>tcsh</TT>



	script file.



<HR>







</DL>







<CENTER>



<H4><A NAME="Heading22<FONT COLOR="#000077">The case Statement</FONT></H4>



</CENTER>



<P>The <TT>case</TT> statement enables you to compare a pattern with several other



patterns and execute a block of code if a match is found. The shell <TT>case</TT>



statement is quite a bit more powerful than the <TT>case</TT> statement in Pascal



or the <TT>switch</TT> statement in C. This is because in the shell <TT>case</TT>



statement you can compare strings with wildcard characters in them, whereas with



the Pascal and C equivalents you can compare only enumerated types or integer values.</P>



<P>Once again, the syntax for the <TT>case</TT> statement is identical for <TT>bash</TT>



and <TT>pdksh</TT> and different for <TT>tcsh</TT>. The syntax for <TT>bash</TT>



and <TT>pdksh</TT> is the following:</P>



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



case string1 in



      str1)



            commands;;



      str2)



            commands;;



      *)



            commands;;



</FONT></PRE>



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



esac



</FONT></PRE>



<P><TT>string1</TT> is compared to <TT>str1</TT> and <TT>str2</TT>. If one of these



strings matches <TT>string1</TT>, the commands up until the double semicolon (<TT>;;</TT>)



are executed. If neither <TT>str1</TT> nor <TT>str2</TT> matches <TT>string1</TT>,



the commands associated with the asterisk are executed. This is the default case



condition because the asterisk matches all strings.</P>



<P>The <TT>tcsh</TT> equivalent of the <TT>bash</TT> and <TT>pdksh</TT> <TT>case</TT>



statement is called the <TT>switch</TT> statement. This statement's syntax closely



follows the C <TT>switch</TT> statement syntax. Here it is:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">switch (string1)



      case  str1:



            statements



      breaksw



      case  str2:



            statements



      breaksw



      default:



            statements



      breaksw







endsw



</FONT></PRE>



<P>This behaves in the same manner as the <TT>bash</TT> and <TT>pdksh</TT> <TT>case</TT>



statement. Each string following the keyword <TT>case</TT> is compared with <TT>string1</TT>.



If any of these strings matches <TT>string1</TT>, the code following it up until



the <TT>breaksw</TT> keyword is executed. If none of the strings matches, the code



following the default keyword up until the <TT>breaksw</TT> keyword is executed.</P>



<P>The following code is an example of a <TT>bash</TT> or <TT>pdksh</TT> <TT>case</TT>



statement. This code checks to see if the first command-line option was <TT>-i</TT>



or <TT>-e</TT>. If it was <TT>-i</TT>, the program counts the number of lines in



the file specified by the second command-line option that begins with the letter



i. If the first option was <TT>-e</TT>, the program counts the number of lines in



the file specified by the second command-line option that begins with the letter



e. If the first command-line option was not <TT>-i</TT> or <TT>-e</TT>, the program



prints a brief error message to the screen.<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">case $1 in



   -i)



      count='grep ^i $2 | wc -l'



      echo &quot;The number of lines in $2 that start with an i is $count&quot;



      ;;



   -e)



      count='grep ^e $2 | wc -l'



      echo &quot;The number of lines in $2 that start with an e is $count&quot;



      ;;



   * )



      echo &quot;That option is not recognized&quot;



      ;;



</FONT></PRE>







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



</FONT></PRE>



<P>The same example written in <TT>tcsh</TT> syntax is shown here:</P>



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



# remember that the first line must start with a # when using tcsh



switch ( $1 )



   case -i | i:



      set count = 'grep ^i $2 | wc -l'



      echo &quot;The number of lines in $2 that begin with i is $count&quot;



   breaksw



   case -e | e:



      set count = 'grep ^e $2 | wc -l'



      echo &quot;The number of lines in $2 that begin with e is $count&quot;



   breaksw



   default:



      echo &quot;That option is not recognized&quot;



   breaksw



</FONT></PRE>



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



endsw



</FONT></PRE>



<CENTER>



<H3><A NAME="Heading23<FONT COLOR="#000077">Iteration Statements</FONT></H3>



</CENTER>



<P>The shell languages also provide several iteration or looping statements. The



most commonly used of these is the <TT>for</TT> statement.



<CENTER>



<H4><A NAME="Heading24<FONT COLOR="#000077">The for Statement</FONT></H4>



</CENTER>



<P>The <TT>for</TT> statement executes the commands that are contained within it



a specified number of times. <TT>bash</TT> and <TT>pdksh</TT> have two variations



of the <TT>for</TT> statement.







<DL>



	<DT></DT>



</DL>











<DL>



	<DD>



<HR>



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



	statement syntax is the same in both <TT>bash</TT> and <TT>pdksh</TT>.



<HR>







</DL>







<P>The first form of <TT>for</TT> statement that <TT>bash</TT> and <TT>pdksh</TT>



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



<PRE><FONT COLOR="#0066FF">for var1 in list



do



      commands



</FONT></PRE>







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



</FONT></PRE>



<P>In this form, the <TT>for</TT> statement executes once for each item in the list.



This list can be a variable that contains several words separated by spaces, or it



can be a list of values that is typed directly into the statement. Each time through



the loop, the variable <TT>var1</TT> is assigned the current item in the list, until



the last one is reached.</P>



<P>The second form of <TT>for</TT> statement has the following syntax:<FONT COLOR="#0066FF"></FONT>



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



do



      statements



</FONT></PRE>







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



</FONT></PRE>



<P>In this form, the <TT>for</TT> statement executes once for each item in the variable



<TT>var1</TT>. When this syntax of the <TT>for</TT> statement is used, the shell



program assumes that the <TT>var1</TT> variable contains all the positional parameters



that were passed in to the shell program on the command line.</P>



<P>Typically this form of <TT>for</TT> statement is the equivalent of writing the



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



<PRE><FONT COLOR="#0066FF">for var1 in &quot;$@&quot;



do



      statements



</FONT></PRE>







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



</FONT></PRE>



<P>The equivalent of the <TT>for</TT> statement in <TT>tcsh</TT> is called the <TT>foreach</TT>



statement. It behaves in the same manner as the <TT>bash</TT> and <TT>pdksh</TT>



<TT>for</TT> statement. The syntax of the <TT>foreach</TT> statement is the following:<FONT



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



<PRE><FONT COLOR="#0066FF">foreach name (list)



      commands







end



</FONT></PRE>



<P>The following is an example of the <TT>bash</TT> or <TT>pdksh</TT> style of <TT>for</TT>



statement. This example takes as command-line options any number of text files. The



program reads in each of these files, converts all the letters to uppercase, and



then stores the results in a file of the same name but with a <TT>.caps</TT> extension.<FONT



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



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



do



tr a-z A-Z &lt; $file &gt;$file.caps



</FONT></PRE>







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



</FONT></PRE>



<P>The same example written in <TT>tcsh</TT> shell language is shown next:<FONT COLOR="#0066FF"></FONT>



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



foreach file ($*)



   tr a-z A-Z &lt; $file &gt;$file.caps



</FONT></PRE>







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



</FONT></PRE>



<CENTER>



<H4><A NAME="Heading26<FONT COLOR="#000077">The while Statement</FONT></H4>



</CENTER>



<P>Another iteration statement offered by the shell programming language is the <TT>while</TT>



statement. This statement causes a block of code to be executed while a provided



conditional expression is true. The syntax for the <TT>while</TT> statement in <TT>bash</TT>



and <TT>pdksh</TT> is the following:<FONT COLOR="#0066FF"></FONT>



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



do



      statements



</FONT></PRE>







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



</FONT></PRE>



<P>The syntax for the <TT>while</TT> statement in <TT>tcsh</TT> is the following:<FONT



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



<PRE><FONT COLOR="#0066FF">while (expression)



      statements



</FONT></PRE>







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



</FONT></PRE>



<P>The following is an example of the <TT>bash</TT> and <TT>pdksh</TT> style of <TT>while</TT>



statement. This program lists the parameters that were passed to the program, along



with the parameter number.<FONT COLOR="#0066FF"></FONT>



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



while [ -n &quot;$*&quot; ]



do



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



      shift



      count='expr $count + 1'







done



</FONT></PRE>



<P>As you will see in the section titled &quot;The <TT>shift</TT> Command,&quot;



the <TT>shift</TT> command moves the command-line parameters over one to the left.</P>



<P>The same program written in the <TT>tcsh</TT> language is shown next:<FONT COLOR="#0066FF"></FONT>



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



set count = 1



while ( &quot;$*&quot; != &quot;&quot; )



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



      shift



      set count = 'expr $count + 1'



</FONT></PRE>







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



</FONT></PRE>



<CENTER>



<H4><A NAME="Heading27<FONT COLOR="#000077">The until Statement</FONT></H4>



</CENTER>



<P>The <TT>until</TT> statement is very similar in syntax and function to the <TT>while</TT>



statement. The only real difference between the two is that the <TT>until</TT> statement



executes its code block while its conditional expression is false, and the <TT>while</TT>



statement executes its code block while its conditional expression is true. The syntax



for the <TT>until</TT> statement in <TT>bash</TT> and <TT>pdksh</TT> is<FONT COLOR="#0066FF"></FONT>



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



do



      commands



</FONT></PRE>







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



</FONT></PRE>



<P>The same example that was used for the <TT>while</TT> statement can be used for



the <TT>until</TT> statement. All you have to do to make it work is negate the condition.



This is shown in the following code:<FONT COLOR="#0066FF"></FONT>



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



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



do



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



      shift



      count='expr $count + 1'



</FONT></PRE>







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



</FONT></PRE>



<P>The only difference between this example and the <TT>while</TT> statement example

⌨️ 快捷键说明

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