jeudi 29 août 2019

Unable to break out of While loop

I would like to break out of a loop when it gets to a blank line in a file. The issue is that my regexp's used to condition my data create a line with characters so I need something from the beginning to check if a line is empty or not so I can break out. What am I missing?


#NOTES: chmod this script with chmod 755 to run as regular local user

#This line allows for passing in a source file as an argument to the script (i.e: ./script.sh source_file.txt)
input_file="$1"

#This creates the folder structure used to mount the SMB Share and copy the assets over to the local machines
SOURCE_FILES_ROOT_DIR="${HOME}/operations/source" 
DESTINATION_FILES_ROOT_DIR="${HOME}/operations/copied_files"

#This creates the fileshare mount point and place to copy files over to on the local machine.
 echo "Creating initial folders..."
 mkdir -p "${SOURCE_FILES_ROOT_DIR}"
 mkdir -p "${DESTINATION_FILES_ROOT_DIR}"
 echo "Folders Created! Destination files will be copied to ${DESTINATION_FILES_ROOT_DIR}/SHARE_NAME"


while read -r line; 
  do  
    if [ -n "$line" ]; then 
        continue
    fi      
    line=${line/\\\\///}
    line=${line//\\//}
    line=${line%%\"*\"}
    SERVER_NAME=$(echo "$line" | cut -d / -f 4);
    SHARE_NAME=$(echo "$line" | cut -d / -f 5);
    ASSET_LOC=$(echo "$line" | cut -d / -f 6-);
    SMB_MOUNT_PATH="//$(whoami)@${SERVER_NAME}/${SHARE_NAME}";

     if df -h | grep -q "${SMB_MOUNT_PATH}"; then
       echo "${SHARE_NAME} is already mounted. Copying files..."
     else
       echo "Mounting it"
       mount_smbfs "${SMB_MOUNT_PATH}" "${SOURCE_FILES_ROOT_DIR}"
      fi
   cp -a ${SOURCE_FILES_ROOT_DIR}/${ASSET_LOC} ${DESTINATION_FILES_ROOT_DIR}

  done < $input_file

# cleanup
 hdiutil unmount ${SOURCE_FILES_ROOT_DIR}

exit 0```

Expected result was for the script to realize when it gets to a blank line and then stops. The script works works when i remove the         `if [ -n "$line" ]; then    
            continue
        fi` The script runs and pulls assets but just keeps on going and never breaks out. When I do it as is now I get :
Creating initial folders...
Folders Created! Destination files will be copied to /Users/baguiar/operations/copied_files
Mounting it
mount_smbfs: server connection failed: No route to host
hdiutil: unmount: "/Users/baguiar/operations/source" failed to unmount due to error 16.
hdiutil: unmount failed - Resource busy

Aucun commentaire:

Enregistrer un commentaire