[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
}
# 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}@"