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

📄 communications.html

📁 一本完整的描述Unix Shell 编程的工具书的所有范例
💻 HTML
📖 第 1 页 / 共 3 页
字号:
  29&nbsp;OPTS="-rtv --delete-excluded --delete-after --partial"  30&nbsp;  31&nbsp;# rsync include pattern  32&nbsp;# Leading slash causes absolute path name match.  33&nbsp;INCLUDE=(  34&nbsp;    "/4/i386/kde-i18n-Chinese*"   35&nbsp;#   ^                         ^  36&nbsp;# Quoting is necessary to prevent globbing.  37&nbsp;)   38&nbsp;  39&nbsp;  40&nbsp;# rsync exclude pattern  41&nbsp;# Temporarily comment out unwanted pkgs using "#" . . .  42&nbsp;EXCLUDE=(  43&nbsp;    /1  44&nbsp;    /2  45&nbsp;    /3  46&nbsp;    /testing  47&nbsp;    /4/SRPMS  48&nbsp;    /4/ppc  49&nbsp;    /4/x86_64  50&nbsp;    /4/i386/debug  51&nbsp;   "/4/i386/kde-i18n-*"  52&nbsp;   "/4/i386/openoffice.org-langpack-*"  53&nbsp;   "/4/i386/*i586.rpm"  54&nbsp;   "/4/i386/GFS-*"  55&nbsp;   "/4/i386/cman-*"  56&nbsp;   "/4/i386/dlm-*"  57&nbsp;   "/4/i386/gnbd-*"  58&nbsp;   "/4/i386/kernel-smp*"  59&nbsp;#  "/4/i386/kernel-xen*"   60&nbsp;#  "/4/i386/xen-*"   61&nbsp;)  62&nbsp;  63&nbsp;  64&nbsp;init () {  65&nbsp;    # Let pipe command return possible rsync error, e.g., stalled network.  66&nbsp;    set -o pipefail  67&nbsp;  68&nbsp;    TMP=${TMPDIR:-/tmp}/${0##*/}.$$     # Store refined download list.  69&nbsp;    trap "{  70&nbsp;        rm -f $TMP 2&#62;/dev/null  71&nbsp;    }" EXIT                             # Clear temporary file on exit.  72&nbsp;}  73&nbsp;  74&nbsp;  75&nbsp;check_pid () {  76&nbsp;# Check if process exists.  77&nbsp;    if [ -s "$PID_FILE" ]; then  78&nbsp;        echo "PID file exists. Checking ..."  79&nbsp;        PID=$(/bin/egrep -o "^[[:digit:]]+" $PID_FILE)  80&nbsp;        if /bin/ps --pid $PID &#38;&#62;/dev/null; then  81&nbsp;            echo "Process $PID found. ${0##*/} seems to be running!"  82&nbsp;           /usr/bin/logger -t ${0##*/} \  83&nbsp;                 "Process $PID found. ${0##*/} seems to be running!"  84&nbsp;            exit $E_RETURN  85&nbsp;        fi  86&nbsp;        echo "Process $PID not found. Start new process . . ."  87&nbsp;    fi  88&nbsp;}  89&nbsp;  90&nbsp;  91&nbsp;#  Set overall file update range starting from root or $URL,  92&nbsp;#+ according to above patterns.  93&nbsp;set_range () {  94&nbsp;    include=  95&nbsp;    exclude=  96&nbsp;    for p in "${INCLUDE[@]}"; do  97&nbsp;        include="$include --include \"$p\""  98&nbsp;    done  99&nbsp; 100&nbsp;    for p in "${EXCLUDE[@]}"; do 101&nbsp;        exclude="$exclude --exclude \"$p\"" 102&nbsp;    done 103&nbsp;} 104&nbsp; 105&nbsp; 106&nbsp;# Retrieve and refine rsync update list. 107&nbsp;get_list () { 108&nbsp;    echo $$ &#62; $PID_FILE || { 109&nbsp;        echo "Can't write to pid file $PID_FILE" 110&nbsp;        exit $E_RETURN 111&nbsp;    } 112&nbsp; 113&nbsp;    echo -n "Retrieving and refining update list . . ." 114&nbsp; 115&nbsp;    # Retrieve list -- 'eval' is needed to run rsync as a single command. 116&nbsp;    # $3 and $4 is the date and time of file creation. 117&nbsp;    # $5 is the full package name. 118&nbsp;    previous= 119&nbsp;    pre_file= 120&nbsp;    pre_date=0 121&nbsp;    eval /bin/nice /usr/bin/rsync \ 122&nbsp;        -r $include $exclude $URL | \ 123&nbsp;        egrep '^dr.x|^-r' | \ 124&nbsp;        awk '{print $3, $4, $5}' | \ 125&nbsp;        sort -k3 | \ 126&nbsp;        { while read line; do 127&nbsp;            # Get seconds since epoch, to filter out obsolete pkgs. 128&nbsp;            cur_date=$(date -d "$(echo $line | awk '{print $1, $2}')" +%s) 129&nbsp;            #  echo $cur_date 130&nbsp; 131&nbsp;            # Get file name. 132&nbsp;            cur_file=$(echo $line | awk '{print $3}') 133&nbsp;            #  echo $cur_file 134&nbsp; 135&nbsp;            # Get rpm pkg name from file name, if possible. 136&nbsp;            if [[ $cur_file == *rpm ]]; then 137&nbsp;                pkg_name=$(echo $cur_file | sed -r -e \ 138&nbsp;                    's/(^([^_-]+[_-])+)[[:digit:]]+\..*[_-].*$/\1/') 139&nbsp;            else 140&nbsp;                pkg_name= 141&nbsp;            fi 142&nbsp;            # echo $pkg_name 143&nbsp; 144&nbsp;            if [ -z "$pkg_name" ]; then   #  If not a rpm file, 145&nbsp;                echo $cur_file &#62;&#62; $TMP    #+ then append to download list. 146&nbsp;            elif [ "$pkg_name" != "$previous" ]; then   # A new pkg found. 147&nbsp;                echo $pre_file &#62;&#62; $TMP                  # Output latest file. 148&nbsp;                previous=$pkg_name                      # Save current. 149&nbsp;                pre_date=$cur_date 150&nbsp;                pre_file=$cur_file 151&nbsp;            elif [ "$cur_date" -gt "$pre_date" ]; then  #  If same pkg, but newer, 152&nbsp;                pre_date=$cur_date                      #+ then update latest pointer. 153&nbsp;                pre_file=$cur_file 154&nbsp;            fi 155&nbsp;            done 156&nbsp;            echo $pre_file &#62;&#62; $TMP                      #  TMP contains ALL 157&nbsp;                                                        #+ of refined list now. 158&nbsp;            # echo "subshell=$BASH_SUBSHELL" 159&nbsp; 160&nbsp;    }       # Bracket required here to let final "echo $pre_file &#62;&#62; $TMP"  161&nbsp;            # Remained in the same subshell ( 1 ) with the entire loop. 162&nbsp; 163&nbsp;    RET=$?  # Get return code of the pipe command. 164&nbsp; 165&nbsp;    [ "$RET" -ne 0 ] &#38;&#38; { 166&nbsp;        echo "List retrieving failed with code $RET" 167&nbsp;        exit $E_RETURN 168&nbsp;    } 169&nbsp; 170&nbsp;    echo "done"; echo 171&nbsp;} 172&nbsp; 173&nbsp;# Real rsync download part. 174&nbsp;get_file () { 175&nbsp; 176&nbsp;    echo "Downloading..." 177&nbsp;    /bin/nice /usr/bin/rsync \ 178&nbsp;        $OPTS \ 179&nbsp;        --filter "merge,+/ $TMP" \ 180&nbsp;        --exclude '*'  \ 181&nbsp;        $URL $DEST     \ 182&nbsp;        | /usr/bin/tee $LOG 183&nbsp; 184&nbsp;    RET=$? 185&nbsp; 186&nbsp;        #  --filter merge,+/ is crucial for the intention.  187&nbsp;        #  + modifier means include and / means absolute path. 188&nbsp;        #  Then sorted list in $TMP will contain ascending dir name and  189&nbsp;        #+ prevent the following --exclude '*' from "shortcutting the circuit."  190&nbsp; 191&nbsp;    echo "Done" 192&nbsp; 193&nbsp;    rm -f $PID_FILE 2&#62;/dev/null 194&nbsp; 195&nbsp;    return $RET 196&nbsp;} 197&nbsp; 198&nbsp;# ------- 199&nbsp;# Main 200&nbsp;init 201&nbsp;check_pid 202&nbsp;set_range 203&nbsp;get_list 204&nbsp;get_file 205&nbsp;RET=$? 206&nbsp;# ------- 207&nbsp; 208&nbsp;if [ "$RET" -eq 0 ]; then 209&nbsp;    /usr/bin/logger -t ${0##*/} "Fedora update mirrored successfully." 210&nbsp;else 211&nbsp;    /usr/bin/logger -t ${0##*/} "Fedora update mirrored with failure code: $RET" 212&nbsp;fi 213&nbsp; 214&nbsp;exit $RET</PRE></TD></TR></TABLE><HR></DIV><P>Using <BCLASS="COMMAND">rcp</B>, <BCLASS="COMMAND">rsync</B>,	      and similar utilities with security implications in a	      shell script may not be advisable. Consider, instead,	      using <BCLASS="COMMAND">ssh</B>, <BCLASS="COMMAND">scp</B>,	      or an <BCLASS="COMMAND">expect</B> script.</P></DD><DT><ANAME="SSHREF"></A><BCLASS="COMMAND">ssh</B></DT><DD><P><TTCLASS="REPLACEABLE"><I>Secure shell</I></TT>, logs onto	      a remote host and executes commands there. This	      secure replacement for <BCLASS="COMMAND">telnet</B>,	      <BCLASS="COMMAND">rlogin</B>, <BCLASS="COMMAND">rcp</B>, and	      <BCLASS="COMMAND">rsh</B> uses identity authentication	      and encryption. See its <ICLASS="EMPHASIS">manpage</I>	      for details.</P><DIVCLASS="EXAMPLE"><HR><ANAME="REMOTE"></A><P><B>Example 12-40. Using ssh</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# remote.bash: Using ssh.   3&nbsp;   4&nbsp;# This example by Michael Zick.   5&nbsp;# Used with permission.   6&nbsp;   7&nbsp;   8&nbsp;#   Presumptions:   9&nbsp;#   ------------  10&nbsp;#   fd-2 isn't being captured ( '2&#62;/dev/null' ).  11&nbsp;#   ssh/sshd presumes stderr ('2') will display to user.  12&nbsp;#  13&nbsp;#   sshd is running on your machine.  14&nbsp;#   For any 'standard' distribution, it probably is,  15&nbsp;#+  and without any funky ssh-keygen having been done.  16&nbsp;  17&nbsp;# Try ssh to your machine from the command line:  18&nbsp;#  19&nbsp;# $ ssh $HOSTNAME  20&nbsp;# Without extra set-up you'll be asked for your password.  21&nbsp;#   enter password  22&nbsp;#   when done,  $ exit  23&nbsp;#  24&nbsp;# Did that work? If so, you're ready for more fun.  25&nbsp;  26&nbsp;# Try ssh to your machine as 'root':  27&nbsp;#  28&nbsp;#   $  ssh -l root $HOSTNAME  29&nbsp;#   When asked for password, enter root's, not yours.  30&nbsp;#          Last login: Tue Aug 10 20:25:49 2004 from localhost.localdomain  31&nbsp;#   Enter 'exit' when done.  32&nbsp;  33&nbsp;#  The above gives you an interactive shell.  34&nbsp;#  It is possible for sshd to be set up in a 'single command' mode,  35&nbsp;#+ but that is beyond the scope of this example.  36&nbsp;#  The only thing to note is that the following will work in  37&nbsp;#+ 'single command' mode.  38&nbsp;  39&nbsp;  40&nbsp;# A basic, write stdout (local) command.  41&nbsp;  42&nbsp;ls -l  43&nbsp;  44&nbsp;# Now the same basic command on a remote machine.  45&nbsp;# Pass a different 'USERNAME' 'HOSTNAME' if desired:  46&nbsp;USER=${USERNAME:-$(whoami)}  47&nbsp;HOST=${HOSTNAME:-$(hostname)}  48&nbsp;  49&nbsp;#  Now excute the above command line on the remote host,  50&nbsp;#+ with all transmissions encrypted.  51&nbsp;  52&nbsp;ssh -l ${USER} ${HOST} " ls -l "  53&nbsp;  54&nbsp;#  The expected result is a listing of your username's home  55&nbsp;#+ directory on the remote machine.  56&nbsp;#  To see any difference, run this script from somewhere  57&nbsp;#+ other than your home directory.  58&nbsp;  59&nbsp;#  In other words, the Bash command is passed as a quoted line  60&nbsp;#+ to the remote shell, which executes it on the remote machine.  61&nbsp;#  In this case, sshd does  ' bash -c "ls -l" '   on your behalf.  62&nbsp;  63&nbsp;#  For information on topics such as not having to enter a  64&nbsp;#+ password/passphrase for every command line, see  65&nbsp;#+    man ssh  66&nbsp;#+    man ssh-keygen  67&nbsp;#+    man sshd_config.  68&nbsp;  69&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Within a loop, <BCLASS="COMMAND">ssh</B> may cause		unexpected behavior. According to a <AHREF="http://groups-beta.google.com/group/comp.unix.shell/msg/dcb446b5fff7d230"TARGET="_top">		Usenet post</A> in the comp.unix shell archives,		<BCLASS="COMMAND">ssh</B> inherits the loop's		<TTCLASS="FILENAME">stdin</TT>. To remedy this, pass		<BCLASS="COMMAND">ssh</B> either the <TTCLASS="OPTION">-n</TT>		or <TTCLASS="OPTION">-f</TT> option.</P><P>Thanks, Jason Bechtel, for pointing this out.</P></TD></TR></TABLE></DIV></DD><DT><BCLASS="COMMAND">scp</B></DT><DD><P><TTCLASS="REPLACEABLE"><I>Secure copy</I></TT>, similar in	      function to <BCLASS="COMMAND">rcp</B>, copies files between	      two different networked machines, but does so using	      authentication, and with a security level similar to	      <BCLASS="COMMAND">ssh</B>.</P></DD></DL></DIV><DIVCLASS="VARIABLELIST"><P><B><ANAME="COMMLOCAL1"></A>Local Network</B></P><DL><DT><ANAME="WRITEREF"></A><BCLASS="COMMAND">write</B></DT><DD><P>This is a utility for terminal-to-terminal communication.	      It allows sending lines from your terminal (console or	      <ICLASS="FIRSTTERM">xterm</I>) to that of another user. The	      <AHREF="system.html#MESGREF">mesg</A> command may, of course,	      be used to disable write access to a terminal</P><P>Since <BCLASS="COMMAND">write</B> is interactive, it	      would not normally find use in a script.</P></DD><DT><BCLASS="COMMAND">netconfig</B></DT><DD><P>A command-line utility for configuring a network adapter	      (using DHCP). This command is native to Red Hat centric Linux	      distros.</P></DD></DL></DIV><DIVCLASS="VARIABLELIST"><P><B><ANAME="COMMMAIL1"></A>Mail</B></P><DL><DT><BCLASS="COMMAND">mail</B></DT><DD><P>Send or read e-mail messages.</P><P>This stripped-down command-line mail client	      works fine as a command embedded in a script.</P><DIVCLASS="EXAMPLE"><HR><ANAME="SELFMAILER"></A><P><B>Example 12-41. A script that mails itself</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/sh   2&nbsp;# self-mailer.sh: Self-mailing script   3&nbsp;   4&nbsp;adr=${1:-`whoami`}     # Default to current user, if not specified.   5&nbsp;#  Typing 'self-mailer.sh wiseguy@superdupergenius.com'   6&nbsp;#+ sends this script to that addressee.   7&nbsp;#  Just 'self-mailer.sh' (no argument) sends the script   8&nbsp;#+ to the person invoking it, for example, bozo@localhost.localdomain.   9&nbsp;#  10&nbsp;#  For more on the ${parameter:-default} construct,  11&nbsp;#+ see the "Parameter Substitution" section  12&nbsp;#+ of the "Variables Revisited" chapter.  13&nbsp;  14&nbsp;# ============================================================================  15&nbsp;  cat $0 | mail -s "Script \"`basename $0`\" has mailed itself to you." "$adr"  16&nbsp;# ============================================================================  17&nbsp;  18&nbsp;# --------------------------------------------  19&nbsp;#  Greetings from the self-mailing script.  20&nbsp;#  A mischievous person has run this script,  21&nbsp;#+ which has caused it to mail itself to you.  22&nbsp;#  Apparently, some people have nothing better  23&nbsp;#+ to do with their time.  24&nbsp;# --------------------------------------------  25&nbsp;  26&nbsp;echo "At `date`, script \"`basename $0`\" mailed to "$adr"."  27&nbsp;  28&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV></DD><DT><BCLASS="COMMAND">mailto</B></DT><DD><P>Similar to the <BCLASS="COMMAND">mail</B> command,	      <BCLASS="COMMAND">mailto</B> sends e-mail messages	      from the command line or in a script. However,	      <BCLASS="COMMAND">mailto</B> also permits sending MIME	      (multimedia) messages.</P></DD><DT><BCLASS="COMMAND">vacation</B></DT><DD><P>This utility automatically replies to e-mails that	      the intended recipient is on vacation and temporarily	      unavailable. This runs on a network, in conjunction with	      <BCLASS="COMMAND">sendmail</B>, and is not applicable to a	      dial-up POPmail account.</P></DD></DL></DIV></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN9739"HREF="communications.html#AEN9739">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P><ANAME="DAEMONREF"></A></P><P>A <ICLASS="EMPHASIS">daemon</I> is a background		    process not attached to a terminal session. Daemons		    perform designated services either at specified times		    or explicitly triggered by certain events.</P><P>The word <SPANCLASS="QUOTE">"daemon"</SPAN> means ghost in		    Greek, and there is certainly something mysterious,		    almost supernatural, about the way UNIX daemons		    silently wander about behind the scenes, carrying		    out their appointed tasks.</P></TD></TR></TABLE><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="filearchiv.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="terminalccmds.html">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">File and Archiving Commands</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="external.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Terminal Control Commands</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

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