Bash 脚本因绝对路径变量中存在空格而失败

Bash 脚本因绝对路径变量中存在空格而失败

我正在运行以下脚本,将文件从系统上安装的 CIFS 共享复制到另一个目标系统。 CIFS 共享的绝对路径包含很少的空格,因此该路径失败,我尝试在另一个不包含空格的路径上运行它,并且工作正常。我声明 CIFS 共享绝对路径的方式似乎存在一些问题:

#!/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

它失败并出现以下错误:

+ '[' '!' -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

答案1

该错误的原因不是空格,而是缺少参数-pmkdir如注释中所述。这有效(仅当目录x已存在时才会引发错误):

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

x但如果其中的任何父目录尚不存在,则将不起作用。它将与-p

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

man mkdir

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

相关内容