私のシステムにマウントされている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
エラーの原因は、コメントで述べたように空白ではなく、-p
引数の欠落です。mkdir
これは機能します(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