From 0e865d90259bba3b99b22a1ed85703bfe47d3605 Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Mon, 21 Oct 2019 14:48:01 +0200 Subject: [PATCH] [ssh_tools] multicopy: use getopts, fix nounset bugs --- ssh_tools/multicopy.sh | 51 ++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 32 deletions(-) diff --git a/ssh_tools/multicopy.sh b/ssh_tools/multicopy.sh index c1e88ed..293065a 100755 --- a/ssh_tools/multicopy.sh +++ b/ssh_tools/multicopy.sh @@ -56,40 +56,26 @@ warn() printf 'Warning! %s\n' "$@" >&2 } -# Parse the optional arguments -while [ $# -gt 2 ] ; do - case $1 in - "-h") - print_usage - exit $EXIT_SUCCESS - ;; - "-l") - LOGIN="$2" - shift - shift - ;; - "-P") - PARALLEL=1 - shift - ;; - "-r") - RSYNC=1 - shift - ;; - "-R") - DELETE=1 - shift - ;; - *) - break - ;; +# Parse CLI options +LOGIN= +PARALLEL= +RSYNC= +DELETE= +while getopts hl:PrR option ; do + case $option in + h) print_usage + exit $EXIT_SUCCESS + ;; + l) LOGIN="$OPTARG" ;; + P) PARALLEL=1 ;; + r) RSYNC=1 ;; + R) DELETE=1 ;; + \?) bad_usage "Bad option." esac done +shift $((OPTIND - 1)) # Do we still have at least a host list and a file? -# Note: if the program was wrongly called but still has 2 arguments, for -# example with "multicopy -l root -P my_file", it will try to use -# "-P" as hosts' list and "my_file" as the file to be transfered. if [ $# -lt 2 ] ; then bad_usage "Wrong number of arguments." fi @@ -106,7 +92,7 @@ fi # Ignore -P if rsync is going to be used if [ "$RSYNC" = "1" ] && [ "$PARALLEL" = "1" ] ; then warn "Cannot transfer in parallel when using rsync: ignoring -P." - PARALLEL="" + PARALLEL= fi # Check dependencies @@ -129,7 +115,7 @@ fi # Hosts' list file HOSTS_LIST_NAME="$1" -if [ -z "$XDG_CONFIG_HOME" ] ; then +if [ -z ${XDG_CONFIG_HOME+x} ] ; then XDG_CONFIG_HOME="$HOME/.config" fi HOSTS="$XDG_CONFIG_HOME/ssh_tools/${HOSTS_LIST_NAME}.lst" @@ -141,6 +127,7 @@ if [ ! -f "$HOSTS" ] ; then fi # Login +SSH_LOGIN= if [ -n "$LOGIN" ] ; then echo "Login: $LOGIN" SSH_LOGIN="${LOGIN}@"