online.sh

来自「Shall高级编程」· Shell 代码 · 共 88 行

SH
88
字号
#!/bin/bash# logon.sh: A quick 'n dirty script to check whether you are on-line yet.umask 177  # Make sure temp files are not world readable.TRUE=1LOGFILE=/var/log/messages#  Note that $LOGFILE must be readable#+ (as root, chmod 644 /var/log/messages).TEMPFILE=temp.$$#  Create a "unique" temp file name, using process id of the script.#     Using 'mktemp' is an alternative.#     For example:#     TEMPFILE=`mktemp temp.XXXXXX`KEYWORD=address#  At logon, the line "remote IP address xxx.xxx.xxx.xxx"#                      appended to /var/log/messages.ONLINE=22USER_INTERRUPT=13CHECK_LINES=100#  How many lines in log file to check.trap 'rm -f $TEMPFILE; exit $USER_INTERRUPT' TERM INT#  Cleans up the temp file if script interrupted by control-c.echowhile [ $TRUE ]  #Endless loop.do  tail -n $CHECK_LINES $LOGFILE> $TEMPFILE  #  Saves last 100 lines of system log file as temp file.  #  Necessary, since newer kernels generate many log messages at log on.  search=`grep $KEYWORD $TEMPFILE`  #  Checks for presence of the "IP address" phrase,  #+ indicating a successful logon.  if [ ! -z "$search" ] #  Quotes necessary because of possible spaces.  then     echo "On-line"     rm -f $TEMPFILE    #  Clean up temp file.     exit $ONLINE  else     echo -n "."        #  The -n option to echo suppresses newline,                        #+ so you get continuous rows of dots.  fi  sleep 1  done  #  Note: if you change the KEYWORD variable to "Exit",#+ this script can be used while on-line#+ to check for an unexpected logoff.# Exercise: Change the script, per the above note,#           and prettify it.exit 0# Nick Drage suggests an alternate method:while true  do ifconfig ppp0 | grep UP 1> /dev/null && echo "connected" && exit 0  echo -n "."   # Prints dots (.....) until connected.  sleep 2done# Problem: Hitting Control-C to terminate this process may be insufficient.#+         (Dots may keep on echoing.)# Exercise: Fix this.# Stephane Chazelas has yet another alternative:CHECK_INTERVAL=1while ! tail -n 1 "$LOGFILE" | grep -q "$KEYWORD"do echo -n .   sleep $CHECK_INTERVALdoneecho "On-line"# Exercise: Discuss the relative strengths and weaknesses#           of each of these various approaches.

⌨️ 快捷键说明

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