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

📄 wgetter2.bash

📁 BASH Shell 编程 经典教程 《高级SHELL脚本编程》中文版
💻 BASH
📖 第 1 页 / 共 2 页
字号:
list_func() #  Gives the user the option to use the -i option to wget,            #+ and a list of URLs.{while [ 1 ]; do   echo "Enter the name of the file containing URL's (press q to change your mind)."   read urlfile   if [ ! -e "$urlfile" ] && [ "$urlfile" != q ]; then       # Look for a file, or the quit option.       echo "That file does not exist!"   elif [ "$urlfile" = q ]; then # Check quit option.       echo "Not using a url list."       return   else      echo "using $urlfile."      echo "If you gave me url's on the command line, I'll use those first."                            # Report wget standard behaviour to the user.      lister=" -i $urlfile" # This is what we want to pass to wget.      return   fidone}cookie_func() # Give the user the option to use a different cookie file.{while [ 1 ]; do   echo "Change the cookies file. Press return if you don't want to change it."   read Cookies   # NB: this is not the same as Cookie, earlier.   # There is an 's' on the end.   # Bit like chocolate chips.   if [ -z "$Cookies" ]; then                 # Escape clause for wusses.      return   elif [ ! -e "$Cookies" ]; then      echo "File does not exist.  Try again." # Keep em going . . .   else       CookiesON=" --load-cookies $Cookies"   # File is good -- let's use it!       return   fidone}run_func(){if [ -z "$OPTARG" ]; then# Test to see if we used the in-line option or the query one.   if [ ! -d "$savePath" ]; then      # In case directory doesn't exist . . .      echo "$savePath does not appear to exist."      echo "Please supply path and filename of saved wget commands:"      read newFile         until [ -f "$newFile" ]; do  # Keep going till we get something.            echo "Sorry, that file does not exist.  Please try again."            # Try really hard to get something.            read newFile         done# -------------------------------------------------------------------------#         if [ -z ( grep wget ${newfile} ) ]; then          # Assume they haven't got the right file and bail out.#         echo "Sorry, that file does not contain wget commands.  Aborting."#         exit#         fi## This is bogus code.# It doesn't actually work.# If anyone wants to fix it, feel free!# -------------------------------------------------------------------------      filePath="${newFile}"   else   echo "Save path is $savePath"      echo "Please enter name of the file which you want to use."      echo "You have a choice of:"      ls $savePath                                    # Give them a choice.      read inFile         until [ -f "$savePath/$inFile" ]; do         # Keep going till we get something.            if [ ! -f "${savePath}/${inFile}" ]; then # If file doesn't exist.               echo "Sorry, that file does not exist.  Please choose from:"               ls $savePath                           # If a mistake is made.               read inFile            fi         done      filePath="${savePath}/${inFile}"  # Make one variable . . .   fielse filePath="${savePath}/${OPTARG}"   # Which can be many things . . .fiif [ ! -f "$filePath" ]; then           # If a bogus file got through.   echo "You did not specify a suitable file."   echo "Run this script with the -${save} option first."   echo "Aborting."   exit $E_NO_SAVEFILEfiecho "Using: $filePath"while read; do    eval $REPLY    echo "Completed: $REPLY"done < $filePath  # Feed the actual file we are using into a 'while' loop.exit}# Fish out any options we are using for the script.# This is based on the demo in "Learning The Bash Shell" (O'Reilly).while getopts ":$save$cook$help$list$runn:$inpu$wopt" optdo  case $opt in     $save) save_func;;   #  Save some wgetter sessions for later.     $cook) cookie_func;; #  Change cookie file.     $help) usage;;       #  Get help.     $list) list_func;;   #  Allow wget to use a list of URLs.     $runn) run_func;;    #  Useful if you are calling wgetter from, for example,                          #+ a cron script.     $inpu) run_func;;    #  When you don't know what your files are named.     $wopt) wopts;;       #  Pass options directly to wget.        \?) echo "Not a valid option."            echo "Use -${wopt} if you want to pass options directly to wget,"            echo "or -${help} for help";;      # Catch anything else.  esacdoneshift $((OPTIND - 1))     # Do funky magic stuff with $#.if [ -z "$1" ] && [ -z "$lister" ]; then                          #  We should be left with at least one URL                          #+ on the command line, unless a list is 			  #+ being used -- catch empty CL's.   echo "No URL's given!  You must enter them on the same line as wgetter2."   echo "E.g.,  wgetter2 http://somesite http://anothersite."   echo "Use $help option for more information."   exit $E_NO_URLS        # Bail out, with appropriate error code.fiURLS=" $@"# Use this so that URL list can be changed if we stay in the option loop.while [ 1 ]; do   # This is where we ask for the most used options.   # (Mostly unchanged from version 1 of wgetter)   if [ -z $curDepth ]; then      Current=""   else Current=" Current value is $curDepth"   fi       echo "How deep should I go? (integer: Default is $depthDefault.$Current)"       read Depth   # Recursion -- how far should we go?       inputB=""    # Reset this to blank on each pass of the loop.       echo "Enter the name of the referring page (default is none)."       read inputB  # Need this for some sites.       echo "Do you want to have the output logged to the terminal"       echo "(y/n, default is yes)?"       read noHide  # Otherwise wget will just log it to a file.       case $noHide in    # Now you see me, now you don't.          y|Y ) hide="";;          n|N ) hide=" -b";;            * ) hide="";;       esac       if [ -z ${Depth} ]; then       #  User accepted either default or current depth,                                      #+ in which case Depth is now empty.          if [ -z ${curDepth} ]; then #  See if a depth was set on a previous iteration.             Depth="$depthDefault"    #  Set the default recursion depth if nothing                                      #+ else to use.          else Depth="$curDepth"      #  Otherwise, set the one we used before.          fi       fi   Recurse=" -l $Depth"               # Set how deep we want to go.   curDepth=$Depth                    # Remember setting for next time.       if [ ! -z $inputB ]; then          RefA=" --referer=$inputB"   # Option to use referring page.       fi   WGETTER="${CommandA}${pattern}${hide}${RefA}${Recurse}${CookiesON}${lister}${Woptions}${URLS}"   #  Just string the whole lot together . . .   #  NB: no embedded spaces.   #  They are in the individual elements so that if any are empty,   #+ we don't get an extra space.   if [ -z "${CookiesON}" ] && [ "$cFlag" = "1" ] ; then       echo "Warning -- can't find cookie file"       # This should be changed, in case the user has opted to not use cookies.   fi   if [ "$Flag" = "S" ]; then      echo "$WGETTER" >> $savePath/wget-${today}      #  Create a unique filename for today, or append to it if it exists.      echo "$inputB" >> $savePath/site-list-${today}      #  Make a list, so it's easy to refer back to,      #+ since the whole command is a bit confusing to look at.      echo "Command saved to the file $savePath/wget-${today}"           # Tell the user.      echo "Referring page URL saved to the file $savePath/site-list-${today}"           # Tell the user.      Saver=" with save option"      # Stick this somewhere, so it appears in the loop if set.   else       echo "*****************"       echo "*****Getting*****"       echo "*****************"       echo ""       echo "$WGETTER"       echo ""       echo "*****************"       eval "$WGETTER"   fi       echo ""       echo "Starting over$Saver."       echo "If you want to stop, press q."       echo "Otherwise, enter some URL's:"       # Let them go again. Tell about save option being set.       read       case $REPLY in                # Need to change this to a 'trap' clause.          q|Q ) exit $E_USER_EXIT;;  # Exercise for the reader?            * ) URLS=" $REPLY";;       esac       echo ""doneexit 0

⌨️ 快捷键说明

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