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

📄 339.html

📁 著名的linux英雄站点的文档打包
💻 HTML
📖 第 1 页 / 共 3 页
字号:
you'll see the output:<br>
<br>
<br>
3<br>
1<br>
<br>
<br>
The output shows that $@ returns three argument values, whereas $* returns only one.<br>
<br>
Recycling Files<br>
<br>
Okay, now that we've covered the basics of handling script arguments, let's see if we can do something useful with them.<br>
<br>
One Microsoft Windows feature for which linux newbies often yearn is the recycle bin. You can obtain Linux applications that provide this feature, such as Deltrree (see http://migas.mine.nu/en_deltree_index.shtml). However, in the true spirit of Linux, we will create our own.<br>
<br>
To do so, let's create a script named del that has these contents:<br>
<br>
<br>
mkdir -p /tmp/$USER/del 2&gt;/dev/null<br>
mv $1 /tmp/$USER/del<br>
<br>
<br>
Here's what the script does. The first command, mkdir, creates a subdirectory of /tmp. This is named after the user running the script. The -p argument instructs mkdir to make a second subdirectory within the first one. The second subdirectory is named del, after the name of the script. The del directory is the recycle bin for its associated user.<br>
<br>
The redirection, 2&gt;/dev/null, suppresses any error messages generated by mkdir. This is necessary because the second and subsequent times that the script is run, the subdirectories will already exist. Attempting to create an existing directory results in an error message, which is best suppressed in order to avoid confusing the user.<br>
<br>
The second command, the mv command, moves the file whose name is given as the first script argument. The file is moved to the directory created by the mkdir command on this, or a previous, invocation of the script.<br>
<br>
You can use this script to non-irrevocably delete a file. For instance, suppose you're tired of the file movies. txt. If you issue the command rm movies.txt, the file is gone forever. Instead, you can issue the command del movies.txt, which moves the unwanted file to your recycle bin. Files in the /tmp directory are usually permanently deleted after an interval established by the system administrator. Until a file is deleted, you can recover it simply by using mv to move it out of the /tmp directory tree.<br>
<br>
Okay, a few important words and warnings are in order here. First, you won't be able to execute the del script in the simple fashion indicated unless you give yourself execute access to the script and place the script in a directory that's on your execution path. Previous articles in this series include instructions on how to do this. A workaround is to issue the command sh del, where del is the path to the del script.<br>
<br>
Second, this script won't allow you to recycle files that you delete via a GUI application, such as GNOME's file manager. The script does its work only if you invoke it.<br>
<br>
Third, bear in mind that files don't remain in /tmp indefinitely; at some point, they will be automatically deleted. Several linux distributions use the tmpwatch program to delete temporary files older than 10 days.<br>
<br>
Fourth and finally, the del script doesn't distinguish identically named files originally residing in distinct directories. For example, if you delete /x/z by using del, you will overwrite any saved copy of /y/z. You can overcome this limitation with a little extra code, if you like. However, use del at your own risk. I don't plan to respond to e-mails concerning lost files (grin).<br>
<br>
Handling More Than Nine Arguments<br>
<br>
Because the shell provides only nine variables representing arguments, you might conclude that it's impossible to access a tenth or subsequent argument. However, that'd be a hasty conclusion. The shift command solves this particular problem. When the shell executes the shift command, it discards the value of $1; it then shifts the value of $2 into $1, the value of $3 into $2, and so on. The value of the tenth argument ends up in $9. Here's proof. Place the following script in a file named tener:<br>
<br>
<br>
shift<br>
echo $9<br>
<br>
<br>
and execute it like this:<br>
<br>
<br>
sh tener 1 2 3 4 5 6 7 8 9 10<br>
<br>
<br>
The output should be the value of the tenth argument, 10.<br>
Handling Many Arguments<br>
<br>
As convenient as shell arguments are, the shell cannot infinitely handle many arguments. Early versions of the Unix shell were quite restrictive in this regard. Today's BASH shell is more powerful, but if you attempt to feed the shell 10,000 arguments, you may find that it fails to fully cooperate. As you might expect, it is possible to circumvent this limitation.<br>
<br>
One technique is to use xargs, a program that runs a specified command on files whose names appear in the program's input stream. Because the file names are sent via the input stream rather than the command line, this technique escapes limitations that restrict the size of the command line or the number of command-line arguments.<br>
<br>
As an example, consider how we might re-implement whatsit using xargs. First, let's use ls to generate a list of files in the current directory and send the list to the standard output device:<br>
<br>
<br>
ls<br>
<br>
<br>
Now, let's send that result to the xargs command:<br>
<br>
<br>
ls | xargs<br>
<br>
<br>
Next, let's specify an argument telling xargs what operation to perform:<br>
<br>
<br>
ls | xargs whatis<br>
<br>
<br>
Finally, let's filter and paginate the output as before:<br>
<br>
<br>
ls | xargs whatis | grep -v 'nothing appropriate' | more<br>
<br>
<br>
The result works like the original whatsit except that it won't melt down if the current directory contains many files.<br>
<br>
The xargs command interprets several useful options. Table Two summarizes them. One of the most useful of xarg's arguments is --interactive. When this option is specified, xargs prompts for user confirmation before executing the specified command.<br>
<br>
Table Two: Options of the xargs Command<br>
<br>
Option Definition<br>
--eof=string Specifies that string denotes the end of input. Processing ceases when the specified string is encountered.<br>
--exit Specifies that execution will terminate if --max-chars limit is exceeded.<br>
--help Prints a summary of command options.<br>
--interactive Specifies that the user must confirm each command action.<br>
--max-args=number Specifies the maximum number of arguments used per command line.<br>
--max-chars=number Specifies the maximum number of characters per command line.<br>
--max-lines=number Specifies the maximum number of input lines used per command line.<br>
--max-procs=number Specifies the number of commands that will be simultaneously executed.<br>
--no-run-if-empty Specifies that the command will not be executed if there is no input.<br>
--null Specifies that a null character, rather than whitespace, separates file names on the input stream.<br>
--replace=string Specifies that string found in the command arguments is to be replaced by file names read from the input stream.<br>
--verbose Specifies that each command will be printed before execution.<br>
--version Specifies that xargs should print its version number and terminate.<br>
<br>
As an example of using this option, consider the following script:<br>
<br>
<br>
ls |grep '.*bak'|xargs --interactive --max-lines=1 rm<br>
<br>
<br>
This script uses ls to list the names of files in the current directory. Then it uses grep to remove lines pertaining to files other than those having names ending with bak. Finally, xargs is used to interactively delete these backup files. The user must respond y in order for a file to be deleted. If you try this example, be sure to do so in a directory that contains no valuable files. A slight error in typing can result in the deletion of many important files.<br>
<br>
Winning Arguments<br>
<br>
Okay, now you know how to have multiple arguments without ever losing a single one. This should make you the envy of debaters everywhere. Next month, we'll consider conditional statements, which let you construct scripts that make decisions.<br>
<br>
Until then, happy arguing!<br>
<br>
Debugging Scripts<br>
<br>
When writing scripts, it's helpful to be able to see the commands being executed. To do so, you can use the set command. For instance, placing the command:<br>
<br>
<br>
set -v<br>
<br>
<br>
as the first line of a script will cause the script to print command lines as they're executed. Another argument of the set command is even more powerful. Placing the command:<br>
<br>
<br>
set -x<br>
<br>
<br>
as the first line of a script will also cause the script to print command lines as they're executed. However, the output will also include the expanded values of each command argument.<br>
<br>
<br>
</FONT><br>
                                      </TD>
                                    </TR>
                                <TR>
                                <TD colSpan=2><FONT 
                                class=middlefont></FONT><BR>
                                        <FONT 
                                class=normalfont>全文结束</FONT> </TD>
                                    </TR>
                                <TR>
                                <TD background="images/dot.gif" tppabs="http://www.linuxhero.com/docs/images/dot.gif" colSpan=2 
                                height=10></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></DIV></TD>
                        <TD vAlign=top width="20%" 
                      background="images/line.gif" tppabs="http://www.linuxhero.com/docs/images/line.gif" rowSpan=2> 
                          <DIV align=center> 
                            <table class=tableoutline cellspacing=1 cellpadding=4 
                        width="100%" align=center border=0>
                              <tr class=firstalt> 
                                <td noWrap background="images/bgline.gif" tppabs="http://www.linuxhero.com/docs/images/bgline.gif" colspan=2 height=21>
                                <font class=normalfont><b>所有分类</b></font></td>
                              </tr>
<tr class=secondalt> <td noWrap width=27%> <font class=normalfont>1:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type1.html" tppabs="http://www.linuxhero.com/docs/type1.html">非技术类</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>2:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type2.html" tppabs="http://www.linuxhero.com/docs/type2.html">基础知识</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>3:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type3.html" tppabs="http://www.linuxhero.com/docs/type3.html">指令大全</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>4:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type4.html" tppabs="http://www.linuxhero.com/docs/type4.html">shell</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>5:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type5.html" tppabs="http://www.linuxhero.com/docs/type5.html">安装启动</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>6:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type6.html" tppabs="http://www.linuxhero.com/docs/type6.html">xwindow</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>7:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type7.html" tppabs="http://www.linuxhero.com/docs/type7.html">kde</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>8:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type8.html" tppabs="http://www.linuxhero.com/docs/type8.html">gnome</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>9:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type9.html" tppabs="http://www.linuxhero.com/docs/type9.html">输入法类</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>10:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type10.html" tppabs="http://www.linuxhero.com/docs/type10.html">美化汉化</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>11:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type11.html" tppabs="http://www.linuxhero.com/docs/type11.html">网络配置</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>12:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type12.html" tppabs="http://www.linuxhero.com/docs/type12.html">存储备份</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>13:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type13.html" tppabs="http://www.linuxhero.com/docs/type13.html">杂项工具</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>14:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type14.html" tppabs="http://www.linuxhero.com/docs/type14.html">编程技术</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>15:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type15.html" tppabs="http://www.linuxhero.com/docs/type15.html">网络安全</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>16:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type16.html" tppabs="http://www.linuxhero.com/docs/type16.html">内核技术</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>17:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type17.html" tppabs="http://www.linuxhero.com/docs/type17.html">速度优化</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>18:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type18.html" tppabs="http://www.linuxhero.com/docs/type18.html">apache</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>19:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type19.html" tppabs="http://www.linuxhero.com/docs/type19.html">email</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>20:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type20.html" tppabs="http://www.linuxhero.com/docs/type20.html">ftp服务</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>21:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type21.html" tppabs="http://www.linuxhero.com/docs/type21.html">cvs服务</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>22:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type22.html" tppabs="http://www.linuxhero.com/docs/type22.html">代理服务</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>23:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type23.html" tppabs="http://www.linuxhero.com/docs/type23.html">samba</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>24:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type24.html" tppabs="http://www.linuxhero.com/docs/type24.html">域名服务</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>25:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type25.html" tppabs="http://www.linuxhero.com/docs/type25.html">网络过滤</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>26:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type26.html" tppabs="http://www.linuxhero.com/docs/type26.html">其他服务</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>27:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type27.html" tppabs="http://www.linuxhero.com/docs/type27.html">nfs</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>28:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type28.html" tppabs="http://www.linuxhero.com/docs/type28.html">oracle</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>29:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type29.html" tppabs="http://www.linuxhero.com/docs/type29.html">dhcp</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>30:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type30.html" tppabs="http://www.linuxhero.com/docs/type30.html">mysql</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>31:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type31.html" tppabs="http://www.linuxhero.com/docs/type31.html">php</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>32:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type32.html" tppabs="http://www.linuxhero.com/docs/type32.html">ldap</a></font></td>    </tr>  </table></td></tr>                            </table>
                          </DIV></TD></TR>
                    <TR vAlign=top>
                        <TD width="80%"> 
                          <DIV align=center><BR>
                          </DIV>
                        </TD></TR></TBODY></TABLE></TD></TR>
                </TABLE></TD></TR>
          </TABLE>
      <TABLE cellSpacing=0 cellPadding=4 width="100%" bgColor=#eeeeee 
        border=0><TBODY>
        <TR>
          <TD width="50%">
              <P><FONT class=middlefont>版权所有 &copy; 2004 <A 
            href="mailto:bjchenxu@sina.com">linux知识宝库</A><BR>
                违者必究. </FONT></P>
            </TD>
          <TD width="50%">
              <DIV align=right><FONT class=middlefont>Powered by: <A 
            href="mailto:bjchenxu@sina.com">Linux知识宝库</A> Version 0.9.0 </FONT></DIV>
            </TD></TR></TBODY></TABLE>
      <CENTER></CENTER></TD></TR>
    </TABLE></CENTER></BODY></HTML>

⌨️ 快捷键说明

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