From dc700aa7208321b7fa5c4dfafd6bc6f531a3e351 Mon Sep 17 00:00:00 2001 From: Andrew Lorimer Date: Tue, 22 May 2018 18:25:29 +1000 Subject: [PATCH] improve argument validation --- duckduckget.sh | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/duckduckget.sh b/duckduckget.sh index b24a2fb..3999cba 100755 --- a/duckduckget.sh +++ b/duckduckget.sh @@ -4,22 +4,33 @@ args=("$@") site=${args[0]} -filetype=${args[1]} +extension=${args[1]} outdir=${args[2]} +filetype="" -if [ $# != 3 ] +if (($# == 0 )) then printf "\n\x1b[31mWrong number of arguments\x1b[0m\n\n" - printf "Usage: duckduckget [site] [filetype] [outdir]\n where [site] is the domain to search for files\n [filetype] is extension without preceeding dot\n [outputdir] is the output directory relative to working directory" + printf "Usage: duckduckget SITE [FILETYPE] [DESTINATION] \n where SITE is the domain to search for files\n [FILETYPE] is extension without preceeding dot\n [DESTINATION] is the output directory relative to working directory" exit 1 fi +if [[ $outdir == "" ]] + then + outdir="`pwd`/" +fi + if ! [[ $outdir =~ /$ ]] then - $outdir=$outdir/ + outdir=$outdir/ +fi + +if [[ $extension != "" ]] + then + filetype="filetype:$extension" fi -urls=`curl -silent https://duckduckgo.com/html/\?q\=site:$site%20filetype:$filetype | grep "${filetype}$" | tr -d ' \t\r' | grep -v '^[0-9]' | awk '{print "http://" $0}'` +urls=`curl -silent https://duckduckgo.com/html/\?q\=site:$site%20$filetype | grep "$extension$" | tr -d ' \t\r' | grep -v '^[0-9]' | awk '{print "http://" $0}'` for url in $urls; do wget --cut-dirs=100 -P $outdir $url -- 2.43.2