shell脚本中的mv命令问题

her*_*fiq 2 linux bash shell

我试图使用shell脚本运行mv命令,但它给了我

mv: cannot stat `/opt/logs/merchantportal/logger.log.20160501.*': No such file or directory
mv: cannot stat `/opt/logs/merchantapi/logger.log.20160501.*': No such file or directory
Run Code Online (Sandbox Code Playgroud)

//这是我的SHELL SCRIPT

#!/bin/bash
now="$(date +'%Y%m%d')"
merchantPortalLogsPath="/opt/logs/merchantportal"
merchantApiLogsPath="/opt/logs/merchantapi"
currentDate="$(date +%Y%m%d)"
olderDate="$(date "+%Y%m%d" -d "1 days ago")"
merchantPortalLogsPathBackup=$merchantPortalLogsPath"."$olderDate
merchantApiLogsPathBackup=$merchantApiLogsPath"."$olderDate

mkdir $merchantPortalLogsPathBackup
mkdir $merchantApiLogsPathBackup
echo $merchantPortalLogsPath"/logger.log."$olderDate".*" $merchantPortalLogsPathBackup"/"
echo $merchantApiLogsPath"/logger.log."$olderDate".*" $merchantApiLogsPathBackup"/"

mv $merchantPortalLogsPath"/logger.log."$olderDate".*" $merchantPortalLogsPathBackup"/"
mv $merchantApiLogsPath"/logger.log."$olderDate".*" $merchantApiLogsPathBackup"/"
Run Code Online (Sandbox Code Playgroud)

//但是目录是成功创造的

在此输入图像描述

kay*_*lum 5

".*"
Run Code Online (Sandbox Code Playgroud)

将*内部双引号放在一起将阻止shell将其视为通配符,而是将其作为文字*字符.相反,更改您的脚本,以便它不会引用*.例如:

mv ${merchantPortalLogsPath}/logger.log.${olderDate}.* ${merchantPortalLogsPathBackup}/
mv ${merchantApiLogsPath}/logger.log.${olderDate}.* ${merchantApiLogsPathBackup}/
Run Code Online (Sandbox Code Playgroud)

注意:从技术上讲,实际应该双引号变量扩展来处理带有空格和其他特殊字符的路径.但我没有表明只关注手头的问题.