Bash Scripts failed due to spaces in absolute path variable

67 views Asked by At

I am running following script to copy files from CIFS share mounted on the system to another destination system. The absolute path of CIFS share contains few spaces and so it fails for that path, I tried running it on another path which doesn't contains spaces and it works fine. It seems some issues with the way I have declared absolute path for CIFS share:

#!/bin/bash
set -x

BASEPATH="/mnt/smbdisks/IT_linux/IT Linux Systems Dev & Support/Testing/Operation/Hello"
ADVICES="World Country State"

make_folder()
{
  if [ ! -d "${1}" ]
  then
    echo "Warning: [${1}] Folder does not exist, trying to create..."
    mkdir "$1"
    if [ $? != 0 ]
    then
      echo "Unable to create folder "${1}" - exiting"
      exit 1
    fi
  fi
}

sync_to_apj()
{
  FROM=$1
  TO=$2
  TUNNEL='ssh -A -i /home/linux/.ssh/id_rsa_hostname [email protected] ssh -q'
  EXCLUDE='--exclude Completed --exclude Failed'

  echo in folder [${BASEPATH}]
  echo "Now running copying from ${FROM}/tmp/ to root@hostname01:/common/shared/test/${TO}/"
  rsync -av -e "${TUNNEL}" "${BASEPATH}/${FROM}/tmp/" root@hostname01:/common/shared/test/${TO}/ ${EXCLUDE}
  if [ $? != 0 ]
  then
    echo "Issue with rsync of $1 advices - exiting"
    exit 3
  fi

  # Set perms to JBOSS.JBOSS on our newly copied files
  echo "   .. and adjusting permssions to jboss.jboss on root@hostname01:/common/shared/test"
  ssh -A -i ~/.ssh/id_rsa_hostname root@hostname01 "ssh -q hostname01.example.com 'chown -R jboss.jboss /common/shared/test'"
}
# Main
echo --- START `date` - $0
echo BASEPATH = ["${BASEPATH}"]
for each_advice in ${ADVICES}
do
   echo "   Syncing ${each_advice}"
   #DEST_ADVICE=`echo ${each_advice} | sed -e 's:$:_advices:g'`
   DEST_ADVICE=`echo ${each_advice}`

   make_folder "${BASEPATH}/${each_advice}/tmp"
   make_folder "${BASEPATH}/${each_advice}/New"

   echo "Moving pdf files from ${each_advice} to ${each_advice}/tmp"
   cd "${BASEPATH}"
   mv ${each_advice}/*.{PDF,pdf} ${each_advice}/tmp 2>/dev/null

   sync_to_apj "${each_advice}" "${DEST_ADVICE}"

   echo "Moving pdf files from ${each_advice}/tmp to ${each_advice}/New"
   cd "${BASEPATH}"
   mv ${each_advice}/tmp/*.{PDF,pdf} ${each_advice}/New 2>/dev/null
done
echo --- DONE `date` - $0

It fails with following error:

+ '[' '!' -d '/mnt/smbdisks/IT_linux/IT Linux Systems Dev & Support/Testing/Operation/Hello/World/tmp' ']'
+ echo 'Warning: [/mnt/smbdisks/IT_linux/IT Linux Systems Dev & Support/Testing/Operation/Hello/World/tmp] Folder does not exist, trying to create...'
+ mkdir '/mnt/smbdisks/IT_linux/IT Linux Systems Dev & Support/Testing/Operation/Hello/World/tmp'
mkdir: cannot create directory `/mnt/smbdisks/IT_linux/IT Linux Systems Dev & Support/Testing/Operation/Hello/World/tmp': No such file or directory
+ '[' 1 '!=' 0 ']'
+ echo 'Unable to create folder /mnt/smbdisks/IT_linux/IT' Linux Systems Dev '&' Support/Testing/Operation/Hello/World/tmp - exiting'
+ exit 1
0

There are 0 answers