fix a few bugs and improve comments

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

45
uas.sh
View File

@@ -1,6 +1,6 @@
#!/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
@@ -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 -<number> to file name in front of suffix
((i++))
FILE_PREFIX_tmp="$FILE_PREFIX"-${i}
ok=n
# If file does not exist yet
else
# 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 will be renamed to prevent overwriting."
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:"