Browse Source

fix a few bugs and improve comments

max.mehl 4 months ago
parent
commit
1786ca62ad
No account linked to committer's email address
1 changed files with 32 additions and 17 deletions
  1. 32
    17
      uas.sh

+ 32
- 17
uas.sh View File

@@ -1,6 +1,6 @@
1 1
 #!/bin/bash
2 2
 ########################################################################
3
-#  Copyright (C) 2018 Max Mehl <mail@mehl.mx>
3
+#  Copyright (C) 2019 Max Mehl <mail@mehl.mx>
4 4
 ########################################################################
5 5
 #  
6 6
 #  This program is free software: you can redistribute it and/or modify
@@ -24,7 +24,7 @@
24 24
 ########################################################################
25 25
 
26 26
 
27
-BASENAME=$(basename $0)
27
+BASENAME=$(basename "$0")
28 28
 
29 29
 # Load config
30 30
 BINDIR="$(dirname "$(readlink -f "$0")")"
@@ -38,34 +38,49 @@ if [ "$1" = "" ]; then
38 38
 fi
39 39
 
40 40
 # Transform to full path
41
+# file.pdf or /path/to/file.pdf
41 42
 FILE="$1"
42
-FILE_NAME=$(basename $FILE)
43
-FILE_PREFIX=${FILE_NAME%.*}
44
-FILE_SUFFIX=$([[ "$FILE_NAME" = *.* ]] && echo ".${FILE_NAME##*.}" || echo '')
43
+# -> file.pdf
44
+FILE_NAME="$(basename "${FILE}")"
45
+# -> file
46
+FILE_PREFIX="${FILE_NAME%.*}"
47
+# .pdf
48
+FILE_SUFFIX="$([[ "$FILE_NAME" = *.* ]] && echo ".${FILE_NAME##*.}" || echo '')"
45 49
 
46
-if [ ! -e "$FILE" ]; then
47
-    echo "File $FILE does not exist locally."
50
+# Check whether file actually exists locally
51
+if [ ! -e "${FILE}" ]; then
52
+    echo "File ${FILE} does not exist locally."
48 53
     exit 1
49 54
 fi
50 55
 
51
-i=1
52
-FILE_PREFIX_tmp="$FILE_PREFIX"
56
+# If the file already exists on the host, append a number and iterate if necessary
57
+i=0
58
+FILE_PREFIX_tmp="${FILE_PREFIX}"
53 59
 while [ "$ok" != "y" ]; do
54
-  DEST=$(echo "$FILE_PREFIX_tmp$FILE_SUFFIX" | sed 's/ /_/g')
55
-  ssh ${SSH_HOST} "test -e ${SSH_PATH}/$DEST"
56
-  if [ $? == 0 ]; then
57
-    FILE_PREFIX_tmp="$FILE_PREFIX"-${i}
60
+  # constuct file name, and replace spaces by underscores
61
+  DEST="$(echo "$FILE_PREFIX_tmp$FILE_SUFFIX" | sed 's/ /_/g')"
62
+  # Check if file is existent remotely
63
+  cmd="test -e \"${SSH_PATH}\"/\"${DEST}\""
64
+  # If file does already exist with this name
65
+  if ssh -q "${SSH_HOST}" "${cmd}"; then
66
+    # iterate number and append -<number> to file name in front of suffix
58 67
     ((i++))
68
+    FILE_PREFIX_tmp="$FILE_PREFIX"-${i}
59 69
     ok=n
70
+  # If file does not exist yet
60 71
   else
61
-    echo "A file with an identical name already exists in the remote directory."
62
-    echo "Your file will be renamed to prevent overwriting."
63
-    echo
72
+    # Only output this info if a rename took place
73
+    if [[ $i -gt 0 ]]; then
74
+      echo "A file with an identical name already exists in the remote directory."
75
+      echo "Your file has been renamed to prevent overwriting."
76
+      echo
77
+    fi
64 78
     ok=y
65 79
   fi
66 80
 done
67 81
 
68
-scp "$FILE" ${SSH_HOST}:${SSH_PATH}/"$DEST"
82
+# Upload file
83
+scp "$FILE" "${SSH_HOST}":"${SSH_PATH}"/"${DEST}"
69 84
 
70 85
 # Output download link
71 86
 echo "File has been uploaded. Download link:"

Loading…
Cancel
Save