📄 339.html
字号:
<TD><IMG height=83 src="images/spacer.gif" tppabs="http://www.linuxhero.com/docs/images/spacer.gif" width=1 border=0></TD></TR>
<TR>
<TD background="images/bgline.gif" tppabs="http://www.linuxhero.com/docs/images/bgline.gif"><IMG height=22
src="images/header_r2_c1.gif" tppabs="http://www.linuxhero.com/docs/images/header_r2_c1.gif" width=296 border=0
name=header_r2_c1></TD>
<TD background="images/bgline.gif" tppabs="http://www.linuxhero.com/docs/images/bgline.gif" colSpan=5>
<DIV align=right><FONT class=normalfont>当前位置:
<A href="index.html" tppabs="http://www.linuxhero.com/docs/index.html">本站首页</A>
<font color="#FF6699">>></font>
<A href="type4.html" tppabs="http://www.linuxhero.com/docs/type4.html">shell</A> | <A href="copyright.html" tppabs="http://www.linuxhero.com/docs/copyright.html">版权说明</A></font></DIV>
</TD>
<TD><IMG height=22 src="images/spacer.gif" tppabs="http://www.linuxhero.com/docs/images/spacer.gif" width=1
border=0></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=10 cellPadding=0 width="100%" bgColor=#ffffff
border=0>
<TR>
<TD>
<TABLE cellSpacing=0 cellPadding=3 width="100%" border=0>
<TR>
<TD vAlign=top align=middle width="60%">
<TABLE cellSpacing=0 cellPadding=0 width="100%"
background="images/back.gif" tppabs="http://www.linuxhero.com/docs/images/back.gif" border=0>
<TBODY>
<TR>
<TD vAlign=top width="80%">
<DIV align=center>
<FORM action="search.html" tppabs="http://www.linuxhero.com/docs/search.html" method=get>
</FORM>
<TABLE cellSpacing=0 cellPadding=0 width="95%"
border=0><TBODY>
<TR>
<TD background="images/bgi.gif" tppabs="http://www.linuxhero.com/docs/images/bgi.gif"
height=30></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=3 width="95%"
align=center border=0>
<TBODY>
<TR>
<TD>
<TABLE cellSpacing=0 cellPadding=3 width="100%"
border=0>
<TBODY>
<TR>
<TD vAlign=top>
<p><FONT class=normalfont><B><font color=blue>设shell自己的隐含变量</font></B></FONT><BR><FONT class=smallfont color=#ff9900>2004-04-23 15:18 pm</FONT><BR><FONT class=normalfont>作者:作者<br>来自:Linux知识宝库<br>联系方式:无名<br><br> shell自己的隐含变量,例如$$等, 还有一个隐含变量是记载上一个命令执行成功与<br>
否的,但我想不起来了。那位大虾知道还望告知!或可以在那里找到相关的说明。<br>
<br>
Shell Scripts with Multiple Arguments<br>
In a business or personal relationship, having multiple arguments is generally unpleasant and therefore to be avoided. However, in the case of the linux shell, having multiple arguments is downright handy. Of course, in the Linux world, the word argument does not refer to a dispute; instead, it refers to a word appearing on the command line following the name of a program or script. Shell scripts that process multiple arguments afford economy and ease of use; you can simply type a command name once and have that command operate on an entire series of arguments. So this month we'll look at incorporating this capability into a home-brew script.<br>
<br>
Shell Arguments<br>
<br>
Here's a simple shell script that demonstrates the use of multiple arguments:<br>
<br>
<br>
echo $1 are not $2<br>
<br>
<br>
If you create a file named simple that contains this script and then execute it like this:<br>
<br>
<br>
sh simple dogs cats<br>
<br>
<br>
you'll see the output:<br>
<br>
<br>
dogs are not cats<br>
<br>
<br>
Let's examine how this shell script works. The echo command simply prints the value of each of its arguments. If you read the June Newbies column dealing with environment variables (located online at http://www.linux-mag.com/2001-06/newbies_01.html), you may recognize that $1 and $2 are variable names. They can be recognized as such due to the dollar sign that appears as the first character of each name.<br>
<br>
More specifically, $1 and $2 are known as shell variables, because the shell sets their value whenever a shell script or command is invoked. The variable $1 holds the first argument (if any) of the command; the variable $2 holds the second argument (if any) of the command.<br>
<br>
So, in the previous example, the variable $1 has the value dogs, and the variable $2 has the value cats. Thus, the output of the echo command is dogs are not cats.<br>
<br>
Suppose the script had read:<br>
<br>
<br>
echo $1 are not $3<br>
<br>
<br>
Notice that the variable named $2 has been replaced by one named $3. If you issued the command:<br>
<br>
<br>
sh simple dogs cats birds<br>
<br>
<br>
you'd see the output:<br>
<br>
<br>
dogs are not birds<br>
<br>
<br>
The variable named $3 has the value of the third argument, birds. The value of the second argument, cats, is not referenced by the script.<br>
<br>
Ready for a curve ball? Try creating this script:<br>
<br>
<br>
echo $1 are not $10<br>
<br>
<br>
Notice in this script that the second variable is $10. If you were to then issue the command:<br>
<br>
<br>
sh simple dogs cats birds ants flies worms skunks turtles fish ocelots<br>
<br>
<br>
you'd see the output:<br>
<br>
<br>
dogs are not dogs0<br>
<br>
<br>
You probably expected to see:<br>
<br>
<br>
dogs are not ocelots<br>
<br>
<br>
The reason for the unexpected output is that the shell defines only nine positional arguments, $1 through $9. The shell interprets the token $10 as $1 followed by the digit zero. Hence, the output is dogs0.<br>
Table One summarizes the shell variables related to arguments, including several variables not yet introduced. Among these is $0, which basically spits back the name of the script or command invoked.<br>
<br>
Table One: Shell Arguments<br>
<br>
Variable value<br>
$0 The name of the shell script or command.<br>
$1-$9 The indicated positional argument. For example, $1 is the first argument.<br>
$# The number of shell arguments.<br>
$@ The value of each shell argument.<br>
$* The values of all shell arguments.<br>
<br>
For example, place the following script in a file named me:<br>
<br>
<br>
echo $0<br>
<br>
<br>
Then, issue the command:<br>
<br>
<br>
sh me<br>
<br>
<br>
The output will consist of the name of the script file:<br>
<br>
<br>
me<br>
<br>
<br>
The value of the shell variable $# is the number of script arguments. For example, place the following script in a file named counter:<br>
<br>
<br>
echo $#<br>
<br>
<br>
If you invoke this script like so:<br>
<br>
<br>
sh counter 1 2 3<br>
<br>
<br>
you'll see the output 3; if you invoke the script like so:<br>
<br>
<br>
sh counter 1 2 3 4 5<br>
<br>
<br>
you'll see the output 5.<br>
<br>
The shell variables $@ and $* are quite similar. Each provides the values of the script arguments. However, $@ provides the value of each argument, whereas $* provides a single value that consists of each shell argument, separated by a space. To witness this distinction firsthand, use the counter script given earlier and construct a new script, tester, that references it:<br>
<br>
<br>
sh counter "$@"<br>
sh counter "$*"<br>
<br>
<br>
If you invoke the new script like so:<br>
<br>
<br>
sh tester 1 2 3<br>
<br>
<br>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -