Browse Source

+ check is src empty, * fixes for names with spaces /A

Anton TETERIN 7 months ago
parent
commit
01b4d0d32a
1 changed files with 41 additions and 33 deletions
  1. 41 33
      scripts/media2import.sh

+ 41 - 33
scripts/media2import.sh

@@ -6,7 +6,7 @@
 #
 
 #
-# Author: Anton TETERIN (https://og2k.com)
+# Author: Anton TETERIN (https://2dz.fi)
 # Source: https://github.com/InstallAndUse/Graphics
 #
 # History:
@@ -18,10 +18,11 @@
 # 2023-06-29  + hash comparison /A
 #             + removal on successful integrity verification /A
 # 2023-06-30  + add note to subdir, if specified /A
+# 2024-02-22  * fixed: destination path with spaces in quotes will break execution /A
+#             + check existance of files in src /A
 #
 
 # TODO
-# 2024-02-09  * destination path with spaces in quotes will break execution /A
 
 
 function ts() {
@@ -54,26 +55,26 @@ done
 
 # read source path, by default current path (if not given)
 # assuming not recursive, only in-directory files will be imported
-if [ -z $src ]; then
+if [ -z "${src}" ]; then
     read -p "[ $(ts) ]:      Source [$(pwd)]: " src
-    if [ -z ${src} ]; then
+    if [ -z "${src}" ]; then
         src="$(pwd)"
     fi
 fi
 
 # read destination path, by default current path (if not given)
-if [ -z $dst ]; then
+if [ -z "${dst}" ]; then
     read -p "[ $(ts) ]: Destination [$(pwd)]: " dst
-    if [ -z ${dst} ]; then
+    if [ -z "${dst}" ]; then
         dst="$(pwd)"
     fi
 fi
 
 # read note, by default empty
-if [ -z $note ]; then
+if [ -z "${note}" ]; then
     read -p "[ $(ts) ]:        Note: " note
     # TODO: warning, if space present.
-    if [ -z ${note} ]; then
+    if [ -z "${note}" ]; then
         note=""
     else
         # adding hyphen before note, if it is not empty
@@ -82,7 +83,7 @@ if [ -z $note ]; then
 fi
 
 # source and destination can not be the same, exit
-if [ ${src} = ${dst} ]; then
+if [ "${src}" = "${dst}" ]; then
     echo "[ $(ts) ]: Source and destination are the same, exiting..."
     exit 2
 fi
@@ -90,20 +91,26 @@ fi
 
 echo "[ $(ts) ]: ----- [ Transfer details ] ---------------------------------------------"
 # check that source directory exists, otherwise - exit
-if [ -d "$src" ]; then
-    # TODO: src, dst total and free disk space before transfer
-    files_src_total_amount=0
-    files_src_total_size=0
-    for file in "$src"/*; do
-        file_size="$( stat -f %z "$file" )"
-        # echo "[ $(ts) ]: file: ${file} adding file_size: $(( $file_size/1024/1024 )) MB."
-        files_src_total_size=$(( $files_src_total_size+$file_size ))
-        files_src_total_amount=$(( $files_src_total_amount+1 ))
-    done
-    echo "[ $(ts) ]:      Source: [${src}]"
-    # TODO: add nice GB figures (need to use awk or bc)
-    echo "[ $(ts) ]: Total of $files_src_total_amount src files, total size is $(( $files_src_total_size/1024/1024 )) MB)."
-
+if [ -d "${src}" ]; then
+    # if source is empty (has no files to import), exit
+    if [ $(ls -1 "${src}" | wc -l) -gt 0 ]; then
+        # TODO: src, dst total and free disk space before transfer
+        files_src_total_amount=0
+        files_src_total_size=0
+        for file in "${src}"/*; do
+            file_size="$( stat -f %z "$file" )"
+            # echo "[ $(ts) ]: file: ${file} adding file_size: $(( $file_size/1024/1024 )) MB."
+            files_src_total_size=$(( $files_src_total_size+$file_size ))
+            files_src_total_amount=$(( $files_src_total_amount+1 ))
+        done
+
+        echo "[ $(ts) ]:      Source: [${src}]"
+        # TODO: add nice GB figures (need to use awk or bc)
+        echo "[ $(ts) ]: Total of $files_src_total_amount src files, total size is $(( $files_src_total_size/1024/1024 )) MB)."
+    else
+        echo "[ $(ts) ]: src dir is empty, nothing to import."
+        exit 2
+    fi
 else
     echo "[ $(ts) ]: src dir does not exist, exiting..."
     exit 2
@@ -113,16 +120,16 @@ echo "[ $(ts) ]: Destination: [${dst}]"
 echo "[ $(ts) ]:        Note: [${note}]"
 
 # confirm
-read -p "[ $(ts) ]: Confirm (Y): " confirm
+read -p "[ $(ts) ]: Confirm 'Y': " confirm
 if [ ${confirm} = "Y" ]; then
     # echo "[ $(ts) ]: Preparing to transfer..."
 
     # check and create destination directory, if needed
-    if ! [ -d "$dst" ]; then
+    if ! [ -d "${dst}" ]; then
         read -p "[ $(ts) ]: dst dir does not exist, do you want to create? (Y)" confirm
         if [ ${confirm} = "Y" ]; then
             # TODO: add correct check if exit code is successful
-            mkdir -v -p -m 700 "$dst"
+            mkdir -v -p -m 700 "${dst}"
             echo "[ $(ts) ]: dst dir created."
         fi
     fi
@@ -133,8 +140,8 @@ if [ ${confirm} = "Y" ]; then
     files_error_filename=()
 
     # itirating files in src
-    for file in "$src"/*; do
-        # echo "[ $(ts) ]: src dir:       ["$src"]"
+    for file in "${src}"/*; do
+        # echo "[ $(ts) ]: src dir:       ["${src}"]"
 
         filename="$(basename "$file")"
         # figure out when is the creation date
@@ -142,14 +149,14 @@ if [ ${confirm} = "Y" ]; then
         file_size="$( stat -f %z "$file" )"
 
         # appending note, if specified
-        if [ -z "$note" ]; then
+        if [ -z "${note}" ]; then
             dst_subdir="${file_mdate}"
         else
             dst_subdir="${file_mdate}${note}"
         fi
 
         # create subdirectory for creation date
-        mkdir -p "$dst"/"$dst_subdir"
+        mkdir -p "${dst}"/"${dst_subdir}"
         # TODO: echo 'show which file is being copied out of total' in the same status line below
         echo "[ $(ts) ]: [${filename}], modification date is: ${file_mdate}, ( $(( ${file_size}/1024/1024 )) MB )"
 
@@ -157,11 +164,11 @@ if [ ${confirm} = "Y" ]; then
         # calulating src hash sum
         src_hash=$( shasum -a 256 "$file" | cut -d ' ' -f 1)
         # echo "[ $(ts) ]: src sha256sum: [${src_hash}]"
-        # echo "[ $(ts) ]: dst subdir: ["$dst"/"$dst_subdir"]"
+        # echo "[ $(ts) ]: dst subdir: ["${dst}"/"${dst_subdir}"]"
 
         # main operation
         # echo "[ $(ts) ] copying.."
-        cp "$file" "$dst"/"$dst_subdir"
+        cp "$file" "${dst}"/"${dst_subdir}"
 
         # calulating dst hash sum
         dst_hash=$( shasum -a 256 "${dst}/${dst_subdir}/${filename}" | cut -d ' ' -f 1)
@@ -185,7 +192,7 @@ if [ ${confirm} = "Y" ]; then
         # files_src[]="${file}]"
 
         # TODO: add copied file with fullpath to array:
-        # files_dst[]="$dst"/"$dst_subdir"/${filename}
+        # files_dst[]="${dst}"/"${dst_subdir}"/${filename}
     done
 
     # TODO: src, dst total and free disk space before transfer
@@ -197,6 +204,7 @@ if [ ${confirm} = "Y" ]; then
     # diskutil unmount /Volumes/empty
 
     # TODO: open latest directory created?
+    # open -a "${dst}"
 
     # TODO: output total amount of files and size
     # itirate files