279-281.html
来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 157 行
HTML
157 行
<HTML>
<HEAD>
<TITLE>Linux Unleashed, Third Edition:Shell Programming</TITLE>
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!--ISBN=0672313723//-->
<!--TITLE=Linux Unleashed, Third Edition//-->
<!--AUTHOR=Tim Parker//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Sams//-->
<!--CHAPTER=14//-->
<!--PAGES=279-281//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="276-278.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="281-284.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading17"></A><FONT COLOR="#000077">The shift Command</FONT></H4>
<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 one position to the left. For example, if the values of the current positional parameters are</P>
<!-- CODE SNIP //-->
<PRE>
$1 = -r $2 = file1 $3 = file2
</PRE>
<!-- END CODE SNIP //-->
<P>and you execute the <TT>shift</TT> command</P>
<!-- CODE SNIP //-->
<PRE>
shift
</PRE>
<!-- END CODE SNIP //-->
<P>the resulting positional parameters are as follows:
</P>
<!-- CODE SNIP //-->
<PRE>
$1 = file1 $2 = file2
</PRE>
<!-- END CODE SNIP //-->
<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 will shift the positional parameters two places:</P>
<!-- CODE SNIP //-->
<PRE>
shift 2
</PRE>
<!-- END CODE SNIP //-->
<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. The following example was written using <TT>bash</TT>, <TT>pdksh</TT> syntax.</P>
<!-- CODE //-->
<PRE>
while [ “$1” ]
do
if [ “$1” = “-i” ] then
infile=”$2”
shift 2
elif [ “$1” = “-o” ]
then
outfile=”$2”
shift 2
else
echo “Program $0 does not recognize option $1”
fi
done
tr a-z A-Z nfile $outfile
</PRE>
<!-- END CODE //-->
<H4 ALIGN="LEFT"><A NAME="Heading18"></A><FONT COLOR="#000077">The select Statement</FONT></H4>
<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 does not actually 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</P>
<!-- CODE SNIP //-->
<PRE>
select menuitem [<I>in list_of_items</I>]
do
<I>commands</I>
done
</PRE>
<!-- END CODE SNIP //-->
<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 <I>list_of_items</I>. This <I>list_of_items</I> 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:</P>
<!-- CODE SNIP //-->
<PRE>
select menuitem in choice1 choice2 choice3
</PRE>
<!-- END CODE SNIP //-->
<P>If the <TT>list_of_items</TT> is not provided, the <TT>select</TT> statement uses the positional parameters just as it does with the <TT>for</TT> statement.</P>
<P>After 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 is the intended selection. If the user enters anything other than <TT>y</TT> or <TT>Y</TT>, the menu is redisplayed.</P>
<!-- CODE //-->
<PRE>
select menuitem in pick1 pick2 pick3
do
echo “Are you sure you want to pick $menuitem”
read res
if [ $res = “y” -o $res = “Y” ]
then
break
fi
done
</PRE>
<!-- END CODE //-->
<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.</P>
<H4 ALIGN="LEFT"><A NAME="Heading19"></A><FONT COLOR="#000077">The repeat Statement</FONT></H4>
<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:</P>
<!-- CODE SNIP //-->
<PRE>
repeat <I>count command</I>
</PRE>
<!-- END CODE SNIP //-->
<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.</P>
<!-- CODE SNIP //-->
<PRE>
#
foreach num ($*)
repeat $num echo -n “.”
echo “”
end
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>Note: </B><BR>Any <TT>repeat</TT> statement can be rewritten as a <TT>while</TT> or <TT>for</TT> statement. The <TT>repeat</TT> syntax is just more convenient.<HR></FONT>
</BLOCKQUOTE>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="276-278.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="281-284.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?