fix a few bugs and improve comments

This commit is contained in:
2019-03-12 15:10:30 +01:00
parent 4c75729fa5
commit 1786ca62ad

49
uas.sh
View File

@@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
######################################################################## ########################################################################
# Copyright (C) 2018 Max Mehl <mail@mehl.mx> # Copyright (C) 2019 Max Mehl <mail@mehl.mx>
######################################################################## ########################################################################
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
@@ -24,7 +24,7 @@
######################################################################## ########################################################################
BASENAME=$(basename $0) BASENAME=$(basename "$0")
# Load config # Load config
BINDIR="$(dirname "$(readlink -f "$0")")" BINDIR="$(dirname "$(readlink -f "$0")")"
@@ -38,34 +38,49 @@ if [ "$1" = "" ]; then
fi fi
# Transform to full path # Transform to full path
# file.pdf or /path/to/file.pdf
FILE="$1" FILE="$1"
FILE_NAME=$(basename $FILE) # -> file.pdf
FILE_PREFIX=${FILE_NAME%.*} FILE_NAME="$(basename "${FILE}")"
FILE_SUFFIX=$([[ "$FILE_NAME" = *.* ]] && echo ".${FILE_NAME##*.}" || echo '') # -> file
FILE_PREFIX="${FILE_NAME%.*}"
# .pdf
FILE_SUFFIX="$([[ "$FILE_NAME" = *.* ]] && echo ".${FILE_NAME##*.}" || echo '')"
if [ ! -e "$FILE" ]; then # Check whether file actually exists locally
echo "File $FILE does not exist locally." if [ ! -e "${FILE}" ]; then
echo "File ${FILE} does not exist locally."
exit 1 exit 1
fi fi
i=1 # If the file already exists on the host, append a number and iterate if necessary
FILE_PREFIX_tmp="$FILE_PREFIX" i=0
FILE_PREFIX_tmp="${FILE_PREFIX}"
while [ "$ok" != "y" ]; do while [ "$ok" != "y" ]; do
DEST=$(echo "$FILE_PREFIX_tmp$FILE_SUFFIX" | sed 's/ /_/g') # constuct file name, and replace spaces by underscores
ssh ${SSH_HOST} "test -e ${SSH_PATH}/$DEST" DEST="$(echo "$FILE_PREFIX_tmp$FILE_SUFFIX" | sed 's/ /_/g')"
if [ $? == 0 ]; then # Check if file is existent remotely
FILE_PREFIX_tmp="$FILE_PREFIX"-${i} 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 -<number> to file name in front of suffix
((i++)) ((i++))
FILE_PREFIX_tmp="$FILE_PREFIX"-${i}
ok=n ok=n
# If file does not exist yet
else else
echo "A file with an identical name already exists in the remote directory." # Only output this info if a rename took place
echo "Your file will be renamed to prevent overwriting." if [[ $i -gt 0 ]]; then
echo 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 ok=y
fi fi
done done
scp "$FILE" ${SSH_HOST}:${SSH_PATH}/"$DEST" # Upload file
scp "$FILE" "${SSH_HOST}":"${SSH_PATH}"/"${DEST}"
# Output download link # Output download link
echo "File has been uploaded. Download link:" echo "File has been uploaded. Download link:"