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
Bela
  • 9
  • 1
  • 1
    Might be possible that the parent directory wasn't created as well, check for that. Make it `mkdir -p` if you want to get the folder's parent directory(ies) created as well – Arvind Oct 08 '20 at 13:55
  • Thanks for the response. Parent directory exist.I can browse through it and list all files inside. – Bela Oct 08 '20 at 14:08
  • 1
    Did you try to scape those blanks characters with a backslash? – Francesco Gasparetto Oct 08 '20 at 14:23
  • Also try enclosing the paths with ""..."" – Raman Sailopal Oct 08 '20 at 14:28
  • Yes I tried, ``` BASEPATH="/mnt/smbdisks/IT_linux1/IT\ Linux\ Systems\ Dev\ \&\ Support/Testing/Operation/Hello" ``` And then error came like: ``` + mkdir '/mnt/smbdisks/IT_linux1/IT\ Linux\ Systems\ Dev\ \&\ Support/Testing/Operation/Hello/World/tmp' mkdir: cannot create directory `/mnt/smbdisks/IT_linux1/IT\\ Linux\\ Systems\\ Dev\\ \\&\\ Support/Testing/Operation/Hello/World/tmp': Invalid argument + '[' 1 '!=' 0 ']' + echo 'Unable to create folder /mnt/smbdisks/IT_linux1/IT\' 'Linux\' 'Systems\' 'Dev\' '\&\' 'Support/Testing/Operation/Hello/World/tmp - exiting' ``` – Bela Oct 08 '20 at 14:37
  • If you execute the command it reports running, `mkdir '/mnt/smbdisks/IT_linux/IT Linux Systems Dev & Support/Testing/Operation/Hello/World/tmp'` (including exactly those quotes), by hand, does it work? – Gordon Davisson Oct 08 '20 at 15:50
  • Save yourself all this hassle and start writing file/path names with underscores instead of spaces. Good luck. – shellter Oct 08 '20 at 16:19
  • The `set -x` quoting in the debug output shows you have your pathnames correctly specified. as `mkdir -p` isn't working, then it is almost certainly that one of the parent directories in that chain has ownership or permissions that are preventing making the bottom level dirs. See https://stackoverflow.com/a/29571473/620097 for a possible solution. Good luck. – shellter Oct 08 '20 at 16:21
  • Look at http://mywiki.wooledge.org/BashFAQ/020, it might help. The best way is to never store the filename and use `find` like in that page. – Nic3500 Oct 09 '20 at 07:10
  • Thanks for your help, it indeed turned out to be ownership and permission issue. as @shellter suggested. Thanks! – Bela Oct 23 '20 at 13:37

0 Answers0