nameget.shon commit fix bugs & add logging to nameget.sh (7b65fee)
   1#! /bin/bash
   2
   3# get a list of urls and corresponding output file names, then parse them to wget
   4
   5LOGTAG="nameget.sh"
   6args=("$@")
   7list=${args[0]}
   8
   9if [[ $list == "" ]]
  10        then
  11                printf  "\n\x1b[31mWrong number of arguments\x1b[0m\n\n"
  12                printf "Usage: nameget LIST [DESTINATION] [ARGS]\n       where LIST is a text file of format [url] [name] (one pair per line)\n             [DESTINATION] is a path appended to the start of each [name] in LIST\n             [ARGS] is passed directly to wget"
  13                exit 1
  14fi
  15
  16if [[ -d ${args[1]} ]]; then                                    # check if args[2] is [DESTINATION] or [ARGS]
  17        outdir=${args[1]}
  18        if [[ ! $outdir =~ /$ ]]; then                  # check if [DESTINATION] has a trailing /
  19            outdir=$outdir/
  20        fi
  21        shift 2
  22else
  23        shift 1
  24fi
  25
  26otherargs=$@                                                                                                    # set aside remaining arguments for passing to wget
  27
  28logger -s -t $LOGTAG "starting downloading files in $list"
  29
  30while read -r url filename tail; do
  31  if wget --cut-dirs=100 -np --accept -e robots=off -O "$outdir$filename" "$url" $otherargs; then
  32                sed '/$filename/d' $list
  33                logger -s -t $LOGTAG "downloaded $filename"
  34        else
  35                logger -s -t $LOGTAG "downloading of $filename from $url failed."
  36        fi
  37done < $list