📄 moreadv.html
字号:
><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 cat /proc/"$pid"/"$OPTION" | xargs -0 echo 2 # Formats output: ^^^^^^^^^^^^^^^ 3 # From Han Holl's fixup of "get-commandline.sh" 4 #+ 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 #!/bin/bash 2 3 # Generates a log file in current directory 4 # from the tail end of /var/log/messages. 5 6 # Note: /var/log/messages must be world readable 7 # if this script invoked by an ordinary user. 8 # #root chmod 644 /var/log/messages 9 10 LINES=5 11 12 ( date; uname -a ) >>logfile 13 # Time and machine name 14 echo ---------------------------------------------------------- >>logfile 15 tail -n $LINES /var/log/messages | xargs | fmt -s >>logfile 16 echo >>logfile 17 echo >>logfile 18 19 exit 0 20 21 # Note: 22 # ---- 23 # As Frank Wang points out, 24 #+ unmatched quotes (either single or double quotes) in the source file 25 #+ may give xargs indigestion. 26 # 27 # He suggests the following substitution for line 15: 28 # tail -n $LINES /var/log/messages | tr -d "\"'" | xargs | fmt -s >>logfile 29 30 31 32 # Exercise: 33 # -------- 34 # Modify this script to track changes in /var/log/messages at intervals 35 #+ of 20 minutes. 36 # 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 #!/bin/bash 2 # copydir.sh 3 4 # Copy (verbose) all files in current directory ($PWD) 5 #+ to directory specified on command line. 6 7 E_NOARGS=65 8 9 if [ -z "$1" ] # Exit if no argument given. 10 then 11 echo "Usage: `basename $0` directory-to-copy-to" 12 exit $E_NOARGS 13 fi 14 15 ls . | xargs -i -t cp ./{} $1 16 # ^^ ^^ ^^ 17 # -t is "verbose" (output command line to stderr) option. 18 # -i is "replace strings" option. 19 # {} is a placeholder for output text. 20 # This is similar to the use of a curly bracket pair in "find." 21 # 22 # List the files in current directory (ls .), 23 #+ pass the output of "ls" as arguments to "xargs" (-i -t options), 24 #+ then copy (cp) these arguments ({}) to new directory ($1). 25 # 26 # The net result is the exact equivalent of 27 #+ cp * $1 28 #+ unless any of the filenames has embedded "whitespace" characters. 29 30 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 #!/bin/bash 2 # kill-byname.sh: Killing processes by name. 3 # Compare this script with kill-process.sh. 4 5 # For instance, 6 #+ try "./kill-byname.sh xterm" -- 7 #+ and watch all the xterms on your desktop disappear. 8 9 # Warning: 10 # ------- 11 # This is a fairly dangerous script. 12 # Running it carelessly (especially as root) 13 #+ can cause data loss and other undesirable effects. 14 15 E_BADARGS=66 16 17 if test -z "$1" # No command line arg supplied? 18 then 19 echo "Usage: `basename $0` Process(es)_to_kill" 20 exit $E_BADARGS 21 fi 22 23 24 PROCESS_NAME="$1" 25 ps ax | grep "$PROCESS_NAME" | awk '{print $1}' | xargs -i kill {} 2&>/dev/null 26 # ^^ ^^ 27 28 # --------------------------------------------------------------- 29 # Notes: 30 # -i is the "replace strings" option to xargs. 31 # The curly brackets are the placeholder for the replacement. 32 # 2&>/dev/null suppresses unwanted error messages. 33 # 34 # Can grep "$PROCESS_NAME" be replaced by pidof "$PROCESS_NAME"? 35 # --------------------------------------------------------------- 36 37 exit $? 38 39 # The "killall" command has the same effect as this script, 40 #+ 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 #!/bin/bash 2 # wf2.sh: Crude word frequency analysis on a text file. 3 4 # Uses 'xargs' to decompose lines of text into single words. 5 # Compare this example to the "wf.sh" script later on. 6 7 8 # Check for input file on command line. 9 ARGS=1 10 E_BADARGS=65 11 E_NOFILE=66 12 13 if [ $# -ne "$ARGS" ] 14 # Correct number of arguments passed to script? 15 then 16 echo "Usage: `basename $0` filename" 17 exit $E_BADARGS 18 fi 19 20 if [ ! -f "$1" ] # Check if file exists. 21 then 22 echo "File \"$1\" does not exist." 23 exit $E_NOFILE 24 fi 25 26 27 28 ######################################################## 29 cat "$1" | xargs -n1 | \ 30 # List the file, one word per line. 31 tr A-Z a-z | \ 32 # Shift characters to lowercase. 33 sed -e 's/\.//g' -e 's/\,//g' -e 's/ /\
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -