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

📄 moreadv.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Complex Commands</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="External Filters, Programs and Commands"HREF="external.html"><LINKREL="PREVIOUS"TITLE="External Filters, Programs and Commands"HREF="external.html"><LINKREL="NEXT"TITLE="Time / Date Commands"HREF="timedate.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="external.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 15. External Filters, Programs and Commands</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="timedate.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="MOREADV"></A>15.2. Complex Commands</H1><DIVCLASS="VARIABLELIST"><P><B><ANAME="CCLISTING1"></A>Commands for more advanced users</B></P><DL><DT><ANAME="FINDREF"></A><BCLASS="COMMAND">find</B></DT><DD><P><ANAME="FINDREF0"></A></P><P>-exec <TTCLASS="REPLACEABLE"><I>COMMAND</I></TT> \;</P><P>Carries out <TTCLASS="REPLACEABLE"><I>COMMAND</I></TT> on	      each file that <BCLASS="COMMAND">find</B> matches.  The	      command sequence terminates with <SPANCLASS="TOKEN">;</SPAN> (the	      <SPANCLASS="QUOTE">";"</SPAN> is <AHREF="escapingsection.html#ESCP">escaped</A> to	      make certain the shell passes it to <BCLASS="COMMAND">find</B>	      literally, without interpreting it as a special character).</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>find ~/ -name '*.txt'</B></TT> <TTCLASS="COMPUTEROUTPUT">/home/bozo/.kde/share/apps/karm/karmdata.txt /home/bozo/misc/irmeyc.txt /home/bozo/test-scripts/1.txt</TT> 	      </PRE></TD></TR></TABLE>	  </P><P><ANAME="CURLYBRACKETSREF"></A></P><P>If <TTCLASS="REPLACEABLE"><I>COMMAND</I></TT> contains	      <SPANCLASS="TOKEN">{}</SPAN>, then <BCLASS="COMMAND">find</B>	      substitutes the full path name of the selected file for	      <SPANCLASS="QUOTE">"{}"</SPAN>.</P><P>          <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;find ~/ -name 'core*' -exec rm {} \;   2&nbsp;# Removes all core dump files from user's home directory.</PRE></TD></TR></TABLE>	  </P><P>	  <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;find /home/bozo/projects -mtime 1   2&nbsp;#  Lists all files in /home/bozo/projects directory tree   3&nbsp;#+ that were modified within the last day.   4&nbsp;#   5&nbsp;#  mtime = last modification time of the target file   6&nbsp;#  ctime = last status change time (via 'chmod' or otherwise)   7&nbsp;#  atime = last access time   8&nbsp;   9&nbsp;DIR=/home/bozo/junk_files  10&nbsp;find "$DIR" -type f -atime +5 -exec rm {} \;  11&nbsp;#                                      ^^  12&nbsp;#  Curly brackets are placeholder for the path name output by "find."  13&nbsp;#  14&nbsp;#  Deletes all files in "/home/bozo/junk_files"  15&nbsp;#+ that have not been accessed in at least 5 days.  16&nbsp;#  17&nbsp;#  "-type filetype", where  18&nbsp;#  f = regular file  19&nbsp;#  d = directory  20&nbsp;#  l = symbolic link, etc.  21&nbsp;#  (The 'find' manpage and info page have complete listings.)</PRE></TD></TR></TABLE>          </P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;find /etc -exec grep '[0-9][0-9]*[.][0-9][0-9]*[.][0-9][0-9]*[.][0-9][0-9]*' {} \;   2&nbsp;   3&nbsp;# Finds all IP addresses (xxx.xxx.xxx.xxx) in /etc directory files.   4&nbsp;# There a few extraneous hits. Can they be filtered out?   5&nbsp;   6&nbsp;# Possibly by:   7&nbsp;   8&nbsp;find /etc -type f -exec cat '{}' \; | tr -c '.[:digit:]' '\n' \   9&nbsp;| grep '^[^.][^.]*\.[^.][^.]*\.[^.][^.]*\.[^.][^.]*$'  10&nbsp;#  11&nbsp;#  [:digit:] is one of the character classes  12&nbsp;#+ introduced with the POSIX 1003.2 standard.   13&nbsp;  14&nbsp;# Thanks, St閜hane Chazelas. </PRE></TD></TR></TABLE></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>The <TTCLASS="OPTION">-exec</TT> option to	      <BCLASS="COMMAND">find</B> should not be confused with the <AHREF="internal.html#EXECREF">exec</A> shell builtin.</P></TD></TR></TABLE></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="EX57"></A><P><B>Example 15-3. <ICLASS="FIRSTTERM">Badname</I>, eliminate file names		in current directory containing bad characters and <AHREF="special-chars.html#WHITESPACEREF">whitespace</A>.</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# badname.sh   3&nbsp;# Delete filenames in current directory containing bad characters.   4&nbsp;   5&nbsp;for filename in *   6&nbsp;do   7&nbsp;  badname=`echo "$filename" | sed -n /[\+\{\;\"\\\=\?~\(\)\&#60;\&#62;\&#38;\*\|\$]/p`   8&nbsp;# badname=`echo "$filename" | sed -n '/[+{;"\=?~()&#60;&#62;&#38;*|$]/p'`  also works.   9&nbsp;# Deletes files containing these nasties:     + { ; " \ = ? ~ ( ) &#60; &#62; &#38; * | $  10&nbsp;#  11&nbsp;  rm $badname 2&#62;/dev/null  12&nbsp;#             ^^^^^^^^^^^ Error messages deep-sixed.  13&nbsp;done  14&nbsp;  15&nbsp;# Now, take care of files containing all manner of whitespace.  16&nbsp;find . -name "* *" -exec rm -f {} \;  17&nbsp;# The path name of the file that _find_ finds replaces the "{}".  18&nbsp;# The '\' ensures that the ';' is interpreted literally, as end of command.  19&nbsp;  20&nbsp;exit 0  21&nbsp;  22&nbsp;#---------------------------------------------------------------------  23&nbsp;# Commands below this line will not execute because of _exit_ command.  24&nbsp;  25&nbsp;# An alternative to the above script:  26&nbsp;find . -name '*[+{;"\\=?~()&#60;&#62;&#38;*|$ ]*' -maxdepth 0 \  27&nbsp;-exec rm -f '{}' \;  28&nbsp;#  The "-maxdepth 0" option ensures that _find_ will not search  29&nbsp;#+ subdirectories below $PWD.  30&nbsp;  31&nbsp;# (Thanks, S.C.)</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="IDELETE"></A><P><B>Example 15-4. Deleting a file by its <ICLASS="FIRSTTERM">inode</I>	        number</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# idelete.sh: Deleting a file by its inode number.   3&nbsp;   4&nbsp;#  This is useful when a filename starts with an illegal character,   5&nbsp;#+ such as ? or -.   6&nbsp;   7&nbsp;ARGCOUNT=1                      # Filename arg must be passed to script.   8&nbsp;E_WRONGARGS=70   9&nbsp;E_FILE_NOT_EXIST=71  10&nbsp;E_CHANGED_MIND=72  11&nbsp;  12&nbsp;if [ $# -ne "$ARGCOUNT" ]  13&nbsp;then  14&nbsp;  echo "Usage: `basename $0` filename"  15&nbsp;  exit $E_WRONGARGS  16&nbsp;fi    17&nbsp;  18&nbsp;if [ ! -e "$1" ]  19&nbsp;then  20&nbsp;  echo "File \""$1"\" does not exist."  21&nbsp;  exit $E_FILE_NOT_EXIST  22&nbsp;fi    23&nbsp;  24&nbsp;inum=`ls -i | grep "$1" | awk '{print $1}'`  25&nbsp;# inum = inode (index node) number of file  26&nbsp;# -----------------------------------------------------------------------  27&nbsp;# Every file has an inode, a record that holds its physical address info.  28&nbsp;# -----------------------------------------------------------------------  29&nbsp;  30&nbsp;echo; echo -n "Are you absolutely sure you want to delete \"$1\" (y/n)? "  31&nbsp;# The '-v' option to 'rm' also asks this.  32&nbsp;read answer  33&nbsp;case "$answer" in  34&nbsp;[nN]) echo "Changed your mind, huh?"  35&nbsp;      exit $E_CHANGED_MIND  36&nbsp;      ;;  37&nbsp;*)    echo "Deleting file \"$1\".";;  38&nbsp;esac  39&nbsp;  40&nbsp;find . -inum $inum -exec rm {} \;  41&nbsp;#                           ^^  42&nbsp;#        Curly brackets are placeholder  43&nbsp;#+       for text output by "find."  44&nbsp;echo "File "\"$1"\" deleted!"  45&nbsp;  46&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P>The <BCLASS="COMMAND">find</B> command also works	      without the <TTCLASS="OPTION">-exec</TT> option.</P><P>	    <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;#  Find suid root files.   3&nbsp;#  A strange suid file might indicate a security hole,   4&nbsp;#+ or even a system intrusion.   5&nbsp;   6&nbsp;directory="/usr/sbin"   7&nbsp;# Might also try /sbin, /bin, /usr/bin, /usr/local/bin, etc.   8&nbsp;permissions="+4000"  # suid root (dangerous!)   9&nbsp;  10&nbsp;  11&nbsp;for file in $( find "$directory" -perm "$permissions" )  12&nbsp;do  13&nbsp;  ls -ltF --author "$file"  14&nbsp;done</PRE></TD></TR></TABLE>	    </P><P>See <AHREF="filearchiv.html#EX48">Example 15-29</A>, <AHREF="special-chars.html#EX58">Example 3-4</A>,	      and <AHREF="loops.html#FINDSTRING">Example 10-9</A> for scripts using	      <BCLASS="COMMAND">find</B>. Its <AHREF="external.html#MANREF">manpage</A> provides more detail	      on this complex and powerful command.</P></DD><DT><ANAME="XARGSREF"></A><BCLASS="COMMAND">xargs</B></DT><DD><P>A filter for feeding arguments to a command, and also	      a tool for assembling the commands themselves. It breaks	      a data stream into small enough chunks for filters and	      commands to process.  Consider it as a powerful replacement	      for <AHREF="commandsub.html#BACKQUOTESREF">backquotes</A>.	      In situations where <AHREF="commandsub.html#COMMANDSUBREF">command	      substitution</A> fails with a <SPANCLASS="ERRORNAME">too	      many arguments</SPAN> error,	      substituting <BCLASS="COMMAND">xargs</B> often	      works.	        <ANAME="AEN9532"HREF="#FTN.AEN9532">[1]</A>	      Normally, <BCLASS="COMMAND">xargs</B> reads from	      <TTCLASS="FILENAME">stdin</TT> or from a pipe, but it can also	      be given the output of a file.</P

⌨️ 快捷键说明

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