From 1786ca62ad0d2fb05d9389bcca9b58bfb29b404b Mon Sep 17 00:00:00 2001 From: "max.mehl" Date: Tue, 12 Mar 2019 15:10:30 +0100 Subject: [PATCH] fix a few bugs and improve comments --- uas.sh | 49 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/uas.sh b/uas.sh index a00b83c..a2d6ad0 100755 --- a/uas.sh +++ b/uas.sh @@ -1,6 +1,6 @@ #!/bin/bash ######################################################################## -# Copyright (C) 2018 Max Mehl +# Copyright (C) 2019 Max Mehl ######################################################################## # # This program is free software: you can redistribute it and/or modify @@ -24,7 +24,7 @@ ######################################################################## -BASENAME=$(basename $0) +BASENAME=$(basename "$0") # Load config BINDIR="$(dirname "$(readlink -f "$0")")" @@ -38,34 +38,49 @@ if [ "$1" = "" ]; then fi # Transform to full path +# file.pdf or /path/to/file.pdf FILE="$1" -FILE_NAME=$(basename $FILE) -FILE_PREFIX=${FILE_NAME%.*} -FILE_SUFFIX=$([[ "$FILE_NAME" = *.* ]] && echo ".${FILE_NAME##*.}" || echo '') +# -> file.pdf +FILE_NAME="$(basename "${FILE}")" +# -> file +FILE_PREFIX="${FILE_NAME%.*}" +# .pdf +FILE_SUFFIX="$([[ "$FILE_NAME" = *.* ]] && echo ".${FILE_NAME##*.}" || echo '')" -if [ ! -e "$FILE" ]; then - echo "File $FILE does not exist locally." +# Check whether file actually exists locally +if [ ! -e "${FILE}" ]; then + echo "File ${FILE} does not exist locally." exit 1 fi -i=1 -FILE_PREFIX_tmp="$FILE_PREFIX" +# If the file already exists on the host, append a number and iterate if necessary +i=0 +FILE_PREFIX_tmp="${FILE_PREFIX}" while [ "$ok" != "y" ]; do - DEST=$(echo "$FILE_PREFIX_tmp$FILE_SUFFIX" | sed 's/ /_/g') - ssh ${SSH_HOST} "test -e ${SSH_PATH}/$DEST" - if [ $? == 0 ]; then - FILE_PREFIX_tmp="$FILE_PREFIX"-${i} + # constuct file name, and replace spaces by underscores + DEST="$(echo "$FILE_PREFIX_tmp$FILE_SUFFIX" | sed 's/ /_/g')" + # Check if file is existent remotely + cmd="test -e \"${SSH_PATH}\"/\"${DEST}\"" + # If file does already exist with this name + if ssh -q "${SSH_HOST}" "${cmd}"; then + # iterate number and append - to file name in front of suffix ((i++)) + FILE_PREFIX_tmp="$FILE_PREFIX"-${i} ok=n + # If file does not exist yet else - echo "A file with an identical name already exists in the remote directory." - echo "Your file will be renamed to prevent overwriting." - echo + # Only output this info if a rename took place + if [[ $i -gt 0 ]]; then + echo "A file with an identical name already exists in the remote directory." + echo "Your file has been renamed to prevent overwriting." + echo + fi ok=y fi done -scp "$FILE" ${SSH_HOST}:${SSH_PATH}/"$DEST" +# Upload file +scp "$FILE" "${SSH_HOST}":"${SSH_PATH}"/"${DEST}" # Output download link echo "File has been uploaded. Download link:"