From f67776d8a9a36099acd99d01648096424ec7485a Mon Sep 17 00:00:00 2001 From: Stuart Cardall Date: Wed, 26 Apr 2017 15:53:31 +0000 Subject: [PATCH] install-ngxblocker: use getopts not getopt long options for the command line are removed. getopt is replaced with the newer getopts (which is compatible with Debian's DASH Shell) * fixes https://github.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker/issues/35 * adds sanity checks for user command line switches * adds -v command line option to print blacklist version --- install-ngxblocker | 99 +++++++++++++++++++++++++++++++++------------- 1 file changed, 72 insertions(+), 27 deletions(-) diff --git a/install-ngxblocker b/install-ngxblocker index d7289dc1e..aaf13677a 100755 --- a/install-ngxblocker +++ b/install-ngxblocker @@ -39,22 +39,36 @@ REPO=https://raw.githubusercontent.com/mitchellkrogza/nginx-ultimate-bad-bot-blo ####### end user configuration ########################## usage() { - local script=$(basename $0) - cat </dev/null; then + printf "$msg absolute path.\n" + exit 1 + fi + ;; + url) if ! echo $arg | grep -E ^http[s]?://[0-9a-zA-Z-]+[.]+[/0-9a-zA-Z.]+ 1>/dev/null; then + printf "$msg url => http[s]://the.url\n" + exit 1 + fi + ;; + none) printf "$msg argument.\n"; exit 1;; + esac +} + get_options() { - local options=$(getopt -o b:c:r:hx --long \ - bots:,conf:,repo:,help,exec -- "$@" 2>/dev/null) + local arg= opts= - if [ $? -ne 0 ]; then - usage - exit 1 - fi + while getopts :b:c:r:xvh opts "$@" + do + if [ -n "${OPTARG}" ]; then + case "$opts" in + r) arg=$(sanitize_url ${OPTARG});; + *) arg=$(sanitize_path ${OPTARG});; + esac + fi - eval set -- "$options" - - while :; do - case "$1" in - -h | --help) usage && exit 1;; - -x | --exec) DRY_RUN=N; shift;; - -b | --bots) BOTS_DIR=$2; shift 2;; - -c | --conf) CONF_DIR=$2; shift 2;; - -r | --repo) REPO=$2; shift 2;; - *) break;; - esac - done + case "$opts" in + b) BOTS_DIR=$arg; check_args $opts path $arg ;; + c) CONF_DIR=$arg; check_args $opts path $arg ;; + r) REPO=$arg; check_args $opts url $arg ;; + x) DRY_RUN=N ;; + v) check_version ;; + h) usage ;; + \?) usage ;; + :) check_args $OPTARG none none ;; + esac + done } wget_opts() {