[ssh_tools] multicopy: use getopts, fix nounset bugs

This commit is contained in:
Matteo Cypriani 2019-10-21 14:48:01 +02:00
parent 5e6359cf8d
commit 0e865d9025
1 changed files with 19 additions and 32 deletions

View File

@ -56,40 +56,26 @@ warn()
printf 'Warning! %s\n' "$@" >&2 printf 'Warning! %s\n' "$@" >&2
} }
# Parse the optional arguments # Parse CLI options
while [ $# -gt 2 ] ; do LOGIN=
case $1 in PARALLEL=
"-h") RSYNC=
print_usage DELETE=
exit $EXIT_SUCCESS while getopts hl:PrR option ; do
;; case $option in
"-l") h) print_usage
LOGIN="$2" exit $EXIT_SUCCESS
shift ;;
shift l) LOGIN="$OPTARG" ;;
;; P) PARALLEL=1 ;;
"-P") r) RSYNC=1 ;;
PARALLEL=1 R) DELETE=1 ;;
shift \?) bad_usage "Bad option."
;;
"-r")
RSYNC=1
shift
;;
"-R")
DELETE=1
shift
;;
*)
break
;;
esac esac
done done
shift $((OPTIND - 1))
# Do we still have at least a host list and a file? # 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 if [ $# -lt 2 ] ; then
bad_usage "Wrong number of arguments." bad_usage "Wrong number of arguments."
fi fi
@ -106,7 +92,7 @@ fi
# Ignore -P if rsync is going to be used # Ignore -P if rsync is going to be used
if [ "$RSYNC" = "1" ] && [ "$PARALLEL" = "1" ] ; then if [ "$RSYNC" = "1" ] && [ "$PARALLEL" = "1" ] ; then
warn "Cannot transfer in parallel when using rsync: ignoring -P." warn "Cannot transfer in parallel when using rsync: ignoring -P."
PARALLEL="" PARALLEL=
fi fi
# Check dependencies # Check dependencies
@ -129,7 +115,7 @@ fi
# Hosts' list file # Hosts' list file
HOSTS_LIST_NAME="$1" HOSTS_LIST_NAME="$1"
if [ -z "$XDG_CONFIG_HOME" ] ; then if [ -z ${XDG_CONFIG_HOME+x} ] ; then
XDG_CONFIG_HOME="$HOME/.config" XDG_CONFIG_HOME="$HOME/.config"
fi fi
HOSTS="$XDG_CONFIG_HOME/ssh_tools/${HOSTS_LIST_NAME}.lst" HOSTS="$XDG_CONFIG_HOME/ssh_tools/${HOSTS_LIST_NAME}.lst"
@ -141,6 +127,7 @@ if [ ! -f "$HOSTS" ] ; then
fi fi
# Login # Login
SSH_LOGIN=
if [ -n "$LOGIN" ] ; then if [ -n "$LOGIN" ] ; then
echo "Login: $LOGIN" echo "Login: $LOGIN"
SSH_LOGIN="${LOGIN}@" SSH_LOGIN="${LOGIN}@"