added nameget.sh
authorAndrew Lorimer <andrew@lorimer.id.au>
Wed, 4 Jul 2018 11:34:02 +0000 (21:34 +1000)
committerAndrew Lorimer <andrew@lorimer.id.au>
Wed, 4 Jul 2018 11:34:02 +0000 (21:34 +1000)
nameget.sh [new file with mode: 0755]
diff --git a/nameget.sh b/nameget.sh
new file mode 100755 (executable)
index 0000000..e8fbbfa
--- /dev/null
@@ -0,0 +1,29 @@
+#! /bin/bash
+
+# get a list of urls and corresponding output file names, then parse them to wget
+
+args=("$@")
+list=${args[0]}
+
+if [[ $list == "" ]]
+       then
+               printf  "\n\x1b[31mWrong number of arguments\x1b[0m\n\n"
+               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"
+               exit 1
+fi
+
+if [[ -d ${args[1]} ]]; then                                   # check if args[2] is [DESTINATION] or [ARGS]
+       outdir=${args[1]}
+       if [[ ! $outdir =~ /$ ]]; then                  # check if [DESTINATION] has a trailing /
+           outdir=$outdir/
+       fi
+       shift 2
+else
+       shift 1
+fi
+
+otherargs=$@                                                                                                   # set aside remaining arguments for passing to wget
+
+while read -r url filename tail; do
+  wget --cut-dirs=100 -r -np -nc --accept -e robots=off -O "$outdir$filename" "$url" $otherargs
+done < $list