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

📄 wgetter2.bash

📁 BASH Shell 编程 经典教程 《高级SHELL脚本编程》中文版
💻 BASH
📖 第 1 页 / 共 2 页
字号:
#!/bin/bash# wgetter2.bash# Author: Little Monster [monster@monstruum.co.uk]# ==> Used in ABS Guide with permission of script author.# ==> This script still needs debugging and fixups (exercise for reader).# ==> It could also use some additional editing in the comments.#  This is wgetter2 --#+ a Bash script to make wget a bit more friendly, and save typing.#  Carefully crafted by Little Monster.#  More or less complete on 02/02/2005.#  If you think this script can be improved,#+ email me at: monster@monstruum.co.uk# ==> and cc: to the author of the ABS Guide, please.#  This script is licenced under the GPL.#  You are free to copy, alter and re-use it,#+ but please don't try to claim you wrote it.#  Log your changes here instead.# =======================================================================# changelog:# 07/02/2005.  Fixups by Little Monster.# 02/02/2005.  Minor additions by Little Monster.#              (See after # +++++++++++ )# 29/01/2005.  Minor stylistic edits and cleanups by author of ABS Guide.#              Added exit error codes.# 22/11/2004.  Finished initial version of second version of wgetter:#              wgetter2 is born.# 01/12/2004.  Changed 'runn' function so it can be run 2 ways --#              either ask for a file name or have one input on the CL.# 01/12/2004.  Made sensible handling of no URL's given.# 01/12/2004.  Made loop of main options, so you don't#              have to keep calling wgetter 2 all the time.#              Runs as a session instead.# 01/12/2004.  Added looping to 'runn' function.#              Simplified and improved.# 01/12/2004.  Added state to recursion setting.#              Enables re-use of previous value.# 05/12/2004.  Modified the file detection routine in the 'runn' function#              so it's not fooled by empty values, and is cleaner.# 01/02/2004.  Added cookie finding routine from later version (which #              isn't ready yet), so as not to have hard-coded paths.# =======================================================================# Error codes for abnormal exit.E_USAGE=67        # Usage message, then quit.E_NO_OPTS=68      # No command-line args entered.E_NO_URLS=69      # No URLs passed to script.E_NO_SAVEFILE=70  # No save filename passed to script.E_USER_EXIT=71    # User decides to quit.#  Basic default wget command we want to use.#  This is the place to change it, if required.#  NB: if using a proxy, set http_proxy = yourproxy in .wgetrc.#  Otherwise delete --proxy=on, below.# ====================================================================CommandA="wget -nc -c -t 5 --progress=bar --random-wait --proxy=on -r"# ====================================================================# --------------------------------------------------------------------# Set some other variables and explain them.pattern=" -A .jpg,.JPG,.jpeg,.JPEG,.gif,.GIF,.htm,.html,.shtml,.php"                    # wget's option to only get certain types of file.                    # comment out if not usingtoday=`date +%F`    # Used for a filename.home=$HOME          # Set HOME to an internal variable.                    # In case some other path is used, change it here.depthDefault=3      # Set a sensible default recursion.Depth=$depthDefault # Otherwise user feedback doesn't tie in properly.RefA=""             # Set blank referring page.Flag=""             #  Default to not saving anything,                    #+ or whatever else might be wanted in future.lister=""           # Used for passing a list of urls directly to wget.Woptions=""         # Used for passing wget some options for itself.inFile=""           # Used for the run function.newFile=""          # Used for the run function.savePath="$home/w-save"Config="$home/.wgetter2rc"                    #  This is where some variables can be stored,                     #+ if permanently changed from within the script.Cookie_List="$home/.cookielist"                    # So we know where the cookies are kept . . .cFlag=""            # Part of the cookie file selection routine.# Define the options available. Easy to change letters here if needed.# These are the optional options; you don't just wait to be asked.save=s   # Save command instead of executing it.cook=c   # Change cookie file for this session.help=h   # Usage guide.list=l   # Pass wget the -i option and URL list.runn=r   # Run saved commands as an argument to the option.inpu=i   # Run saved commands interactively.wopt=w   # Allow to enter options to pass directly to wget.# --------------------------------------------------------------------if [ -z "$1" ]; then   # Make sure we get something for wget to eat.   echo "You must at least enter a URL or option!"   echo "-$help for usage."   exit $E_NO_OPTSfi# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++# added added added added added added added added added added added addedif [ ! -e "$Config" ]; then   # See if configuration file exists.   echo "Creating configuration file, $Config"   echo "# This is the configuration file for wgetter2" > "$Config"   echo "# Your customised settings will be saved in this file" >> "$Config"else   source $Config             # Import variables we set outside the script.fiif [ ! -e "$Cookie_List" ]; then   # Set up a list of cookie files, if there isn't one.   echo "Hunting for cookies . . ."   find -name cookies.txt >> $Cookie_List   # Create the list of cookie files.fi #  Isolate this in its own 'if' statement,   #+ in case we got interrupted while searching.if [ -z "$cFlag" ]; then # If we haven't already done this . . .   echo                  # Make a nice space after the command prompt.   echo "Looks like you haven't set up your source of cookies yet."   n=0                   # Make sure the counter doesn't contain random values.   while read; do      Cookies[$n]=$REPLY # Put the cookie files we found into an array.      echo "$n) ${Cookies[$n]}"  # Create a menu.      n=$(( n + 1 ))     # Increment the counter.   done < $Cookie_List   # Feed the read statement.   echo "Enter the number of the cookie file you want to use."   echo "If you won't be using cookies, just press RETURN."   echo   echo "I won't be asking this again. Edit $Config"   echo "If you decide to change at a later date"   echo "or use the -${cook} option for per session changes."   read   if [ ! -z $REPLY ]; then   # User didn't just press return.      Cookie=" --load-cookies ${Cookies[$REPLY]}"      # Set the variable here as well as in the config file.      echo "Cookie=\" --load-cookies ${Cookies[$REPLY]}\"" >> $Config   fi   echo "cFlag=1" >> $Config  # So we know not to ask again.fi# end added section end added section end added section end added section end# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++# Another variable.# This one may or may not be subject to variation.# A bit like the small print.CookiesON=$Cookie# echo "cookie file is $CookiesON" # For debugging.# echo "home is ${home}"           # For debugging. Got caught with this one!wopts(){echo "Enter options to pass to wget."echo "It is assumed you know what you're doing."echoecho "You can pass their arguments here too."# That is to say, everything passed here is passed to wget.read Wopts# Read in the options to be passed to wget.Woptions=" $Wopts"# Assign to another variable.# Just for fun, or something . . .echo "passing options ${Wopts} to wget"# Mainly for debugging.# Is cute.return}save_func(){echo "Settings will be saved."if [ ! -d $savePath ]; then  #  See if directory exists.   mkdir $savePath           #  Create the directory to save things in                             #+ if it isn't already there.fiFlag=S# Tell the final bit of code what to do.# Set a flag since stuff is done in main.return}usage() # Tell them how it works.{    echo "Welcome to wgetter.  This is a front end to wget."    echo "It will always run wget with these options:"    echo "$CommandA"    echo "and the pattern to match: $pattern (which you can change at the top of this script)."    echo "It will also ask you for recursion depth, and if you want to use a referring page."    echo "Wgetter accepts the following options:"    echo ""    echo "-$help : Display this help."    echo "-$save : Save the command to a file $savePath/wget-($today) instead of running it."    echo "-$runn : Run saved wget commands instead of starting a new one --"    echo "Enter filename as argument to this option."    echo "-$inpu : Run saved wget commands interactively --"    echo "The script will ask you for the filename."    echo "-$cook : Change the cookies file for this session."    echo "-$list : Tell wget to use URL's from a list instead of from the command line."    echo "-$wopt : Pass any other options direct to wget."    echo ""    echo "See the wget man page for additional options you can pass to wget."    echo ""    exit $E_USAGE  # End here. Don't process anything else.}

⌨️ 快捷键说明

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