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

📄 moreadv.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 3 页
字号:
><P>The default command for <BCLASS="COMMAND">xargs</B> is	      <AHREF="internal.html#ECHOREF">echo</A>. This means that input	      piped to <BCLASS="COMMAND">xargs</B> may have linefeeds and	      other whitespace characters stripped out.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>ls -l</B></TT> <TTCLASS="COMPUTEROUTPUT">total 0 -rw-rw-r--    1 bozo  bozo         0 Jan 29 23:58 file1 -rw-rw-r--    1 bozo  bozo         0 Jan 29 23:58 file2</TT>    <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>ls -l | xargs</B></TT> <TTCLASS="COMPUTEROUTPUT">total 0 -rw-rw-r-- 1 bozo bozo 0 Jan 29 23:58 file1 -rw-rw-r-- 1 bozo bozo 0 Jan...</TT>    <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>find ~/mail -type f | xargs grep "Linux"</B></TT> <TTCLASS="COMPUTEROUTPUT">./misc:User-Agent: slrn/0.9.8.1 (Linux) ./sent-mail-jul-2005: hosted by the Linux Documentation Project. ./sent-mail-jul-2005: (Linux Documentation Project Site, rtf version) ./sent-mail-jul-2005: Subject: Criticism of Bozo's Windows/Linux article ./sent-mail-jul-2005: while mentioning that the Linux ext2/ext3 filesystem . . .</TT> 	      </PRE></TD></TR></TABLE>	      </P><P><TTCLASS="USERINPUT"><B>ls | xargs -p -l gzip</B></TT> <AHREF="filearchiv.html#GZIPREF">gzips</A> every file in current	      directory, one at a time, prompting before each	      operation.</P><P><ANAME="XARGSONEATATIME"></A></P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Note that <ICLASS="FIRSTTERM">xargs</I> processes the	      arguments passed to it sequentially, <SPANCLASS="emphasis"><ICLASS="EMPHASIS">one at	      a time</I></SPAN>.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>find /usr/bin | xargs file</B></TT> <TTCLASS="COMPUTEROUTPUT">/usr/bin:          directory /usr/bin/foomatic-ppd-options:          perl script text executable . . .</TT> 	      </PRE></TD></TR></TABLE>	      </P></TD></TR></TABLE></DIV><P><ANAME="XARGSLIMARGS"></A></P><DIVCLASS="TIP"><TABLECLASS="TIP"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/tip.png"HSPACE="5"ALT="Tip"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>An interesting <ICLASS="FIRSTTERM">xargs</I>	      option is <TTCLASS="OPTION">-n <TTCLASS="REPLACEABLE"><I>NN</I></TT></TT>,	      which limits to <TTCLASS="REPLACEABLE"><I>NN</I></TT> the number	      of arguments passed.</P><P><TTCLASS="USERINPUT"><B>ls | xargs -n 8 echo</B></TT> lists the files in the	      current directory in <TTCLASS="LITERAL">8</TT> columns.</P></TD></TR></TABLE></DIV><P><ANAME="XARGSWS"></A></P><DIVCLASS="TIP"><TABLECLASS="TIP"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/tip.png"HSPACE="5"ALT="Tip"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Another useful option is	      <TTCLASS="OPTION">-0</TT>, in combination with <TTCLASS="USERINPUT"><B>find	      -print0</B></TT> or <TTCLASS="USERINPUT"><B>grep -lZ</B></TT>. This	      allows handling arguments containing whitespace or	      quotes.</P><P>	    <TTCLASS="USERINPUT"><B>find / -type f -print0 | xargs -0 grep -liwZ GUI | xargs -0 rm -f</B></TT>	    </P><P>	    <TTCLASS="USERINPUT"><B>grep -rliwZ GUI / | xargs -0 rm -f</B></TT>	    </P><P>Either of the above will remove any file containing <SPANCLASS="QUOTE">"GUI"</SPAN>.	      <SPANCLASS="emphasis"><ICLASS="EMPHASIS">(Thanks, S.C.)</I></SPAN></P><P>Or:	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;cat /proc/"$pid"/"$OPTION" | xargs -0 echo   2&nbsp;#  Formats output:         ^^^^^^^^^^^^^^^   3&nbsp;#  From Han Holl's fixup of "get-commandline.sh"   4&nbsp;#+ script in "/dev and /proc" chapter.</PRE></TD></TR></TABLE></P></TD></TR></TABLE></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="EX41"></A><P><B>Example 15-5. Logfile: Using <ICLASS="FIRSTTERM">xargs</I> to monitor system log</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;# Generates a log file in current directory   4&nbsp;# from the tail end of /var/log/messages.   5&nbsp;   6&nbsp;# Note: /var/log/messages must be world readable   7&nbsp;# if this script invoked by an ordinary user.   8&nbsp;#         #root chmod 644 /var/log/messages   9&nbsp;  10&nbsp;LINES=5  11&nbsp;  12&nbsp;( date; uname -a ) &#62;&#62;logfile  13&nbsp;# Time and machine name  14&nbsp;echo ---------------------------------------------------------- &#62;&#62;logfile  15&nbsp;tail -n $LINES /var/log/messages | xargs | fmt -s &#62;&#62;logfile  16&nbsp;echo &#62;&#62;logfile  17&nbsp;echo &#62;&#62;logfile  18&nbsp;  19&nbsp;exit 0  20&nbsp;  21&nbsp;#  Note:  22&nbsp;#  ----  23&nbsp;#  As Frank Wang points out,  24&nbsp;#+ unmatched quotes (either single or double quotes) in the source file  25&nbsp;#+ may give xargs indigestion.  26&nbsp;#  27&nbsp;#  He suggests the following substitution for line 15:  28&nbsp;#  tail -n $LINES /var/log/messages | tr -d "\"'" | xargs | fmt -s &#62;&#62;logfile  29&nbsp;  30&nbsp;  31&nbsp;  32&nbsp;#  Exercise:  33&nbsp;#  --------  34&nbsp;#  Modify this script to track changes in /var/log/messages at intervals  35&nbsp;#+ of 20 minutes.  36&nbsp;#  Hint: Use the "watch" command. </PRE></TD></TR></TABLE><HR></DIV><P><ANAME="XARGSCURLYREF"></A></P><P><AHREF="moreadv.html#CURLYBRACKETSREF">As in	      <BCLASS="COMMAND">find</B></A>, a curly bracket	      pair serves as a placeholder for replacement text.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX42"></A><P><B>Example 15-6. Copying files in current directory to another</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# copydir.sh   3&nbsp;   4&nbsp;#  Copy (verbose) all files in current directory ($PWD)   5&nbsp;#+ to directory specified on command line.   6&nbsp;   7&nbsp;E_NOARGS=65   8&nbsp;   9&nbsp;if [ -z "$1" ]   # Exit if no argument given.  10&nbsp;then  11&nbsp;  echo "Usage: `basename $0` directory-to-copy-to"  12&nbsp;  exit $E_NOARGS  13&nbsp;fi    14&nbsp;  15&nbsp;ls . | xargs -i -t cp ./{} $1  16&nbsp;#            ^^ ^^      ^^  17&nbsp;#  -t is "verbose" (output command line to stderr) option.  18&nbsp;#  -i is "replace strings" option.  19&nbsp;#  {} is a placeholder for output text.  20&nbsp;#  This is similar to the use of a curly bracket pair in "find."  21&nbsp;#  22&nbsp;#  List the files in current directory (ls .),  23&nbsp;#+ pass the output of "ls" as arguments to "xargs" (-i -t options),  24&nbsp;#+ then copy (cp) these arguments ({}) to new directory ($1).    25&nbsp;#  26&nbsp;#  The net result is the exact equivalent of  27&nbsp;#+   cp * $1  28&nbsp;#+ unless any of the filenames has embedded "whitespace" characters.  29&nbsp;  30&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="KILLBYNAME"></A><P><B>Example 15-7. Killing processes by name</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# kill-byname.sh: Killing processes by name.   3&nbsp;# Compare this script with kill-process.sh.   4&nbsp;   5&nbsp;#  For instance,   6&nbsp;#+ try "./kill-byname.sh xterm" --   7&nbsp;#+ and watch all the xterms on your desktop disappear.   8&nbsp;   9&nbsp;#  Warning:  10&nbsp;#  -------  11&nbsp;#  This is a fairly dangerous script.  12&nbsp;#  Running it carelessly (especially as root)  13&nbsp;#+ can cause data loss and other undesirable effects.  14&nbsp;  15&nbsp;E_BADARGS=66  16&nbsp;  17&nbsp;if test -z "$1"  # No command line arg supplied?  18&nbsp;then  19&nbsp;  echo "Usage: `basename $0` Process(es)_to_kill"  20&nbsp;  exit $E_BADARGS  21&nbsp;fi  22&nbsp;  23&nbsp;  24&nbsp;PROCESS_NAME="$1"  25&nbsp;ps ax | grep "$PROCESS_NAME" | awk '{print $1}' | xargs -i kill {} 2&#38;&#62;/dev/null  26&nbsp;#                                                       ^^      ^^  27&nbsp;  28&nbsp;# ---------------------------------------------------------------  29&nbsp;# Notes:  30&nbsp;# -i is the "replace strings" option to xargs.  31&nbsp;# The curly brackets are the placeholder for the replacement.  32&nbsp;# 2&#38;&#62;/dev/null suppresses unwanted error messages.  33&nbsp;#  34&nbsp;# Can  grep "$PROCESS_NAME" be replaced by pidof "$PROCESS_NAME"?  35&nbsp;# ---------------------------------------------------------------  36&nbsp;  37&nbsp;exit $?  38&nbsp;  39&nbsp;#  The "killall" command has the same effect as this script,  40&nbsp;#+ but using it is not quite as educational.</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="WF2"></A><P><B>Example 15-8. Word frequency analysis using	      <ICLASS="FIRSTTERM">xargs</I></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# wf2.sh: Crude word frequency analysis on a text file.   3&nbsp;   4&nbsp;# Uses 'xargs' to decompose lines of text into single words.   5&nbsp;# Compare this example to the "wf.sh" script later on.   6&nbsp;   7&nbsp;   8&nbsp;# Check for input file on command line.   9&nbsp;ARGS=1  10&nbsp;E_BADARGS=65  11&nbsp;E_NOFILE=66  12&nbsp;  13&nbsp;if [ $# -ne "$ARGS" ]  14&nbsp;# Correct number of arguments passed to script?  15&nbsp;then  16&nbsp;  echo "Usage: `basename $0` filename"  17&nbsp;  exit $E_BADARGS  18&nbsp;fi  19&nbsp;  20&nbsp;if [ ! -f "$1" ]       # Check if file exists.  21&nbsp;then  22&nbsp;  echo "File \"$1\" does not exist."  23&nbsp;  exit $E_NOFILE  24&nbsp;fi  25&nbsp;  26&nbsp;  27&nbsp;  28&nbsp;########################################################  29&nbsp;cat "$1" | xargs -n1 | \  30&nbsp;#  List the file, one word per line.   31&nbsp;tr A-Z a-z | \  32&nbsp;#  Shift characters to lowercase.  33&nbsp;sed -e 's/\.//g'  -e 's/\,//g' -e 's/ /\

⌨️ 快捷键说明

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