0

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 root@hostname01.exampple.com 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
2
  • 1
    Are you sure the problem is due to the spaces? I haven't reviewed the whole script, but the call to mkdir looks correct. The most likely explanation is that /mnt/smbdisks/IT_linux/IT Linux Systems Dev & Support/Testing/Operation/Hello/World does not exist. Please move cd "${BASEPATH}" above the make_folder` call and try again. – Gilles 'SO- stop being evil' Oct 8 '20 at 15:06
  • 2
    Can you get it fixed by changing mkdir to mkdir -p? – z.h. Oct 8 '20 at 15:07
2

The reason for that error is not the spaces but the absence of the -p parameter for mkdir, as noted in comments. This works (would raise error only if directory x exists already):

x="dir with spaces"
mkdir "$x"

But will not work if any of the parent directories in x does not already exist. It will work with -p:

x="path/to/directory"
> mkdir "$x"
mkdir: cannot create directory ‘path/to/directory’: No such file or directory
> mkdir -p "$x" # working.

from man mkdir

   -p, --parents
          no error if existing, make parent directories as needed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.