我使用Ubuntu16.04(异种)和Bash 4.3.48(1)。
基于Stephen 在本届会议上的回答,我尝试运行以下命令,作为名为imb()
的函数的一部分:
mysqldump -u root -p --all-databases | zip "$drt/db-$date.zip" -
我获取了imb()
函数所在的文件:
source ~/functions.sh
imb() {
drt="/var/www/html"
date="$(date +\%F-\%T)"
mysqldump -u root -p --all-databases | zip "$drt/db-$date.zip" - # Note the hyphen before this comment;
zip -r "all_zipped-$date.zip" "$drt"/ -x "*/cache/*"
rm -f "$drt/db-$date.zip"
}
由于zip
错误,调用函数失败:
zip error: Nothing to do! (/var/www/html/db-2018-04-13-22:27:47.zip)
这似乎是因为连字符在某种程度上忽略了;我认为从以下几个方面来看:
type -a imb
其中产出:
imb is a function
imb () {
date="$(date +\%F-\%T)";
mysqldump -u root -p --all-databases | zip "$drt/db-$date.zip";
zip -r "all_zipped-$date.zip" "$drt"/ -x "*/cache/*";
rm -f "$drt/db-$date.zip"
}
我注意到这里失踪的连字符。
为什么函数失败/为什么Bash据称忽略连字符?
发布于 2018-04-16 16:03:08
您在您的环境中有一个旧版本的函数,并且在编辑它以添加缺少的-
之后,没有重新获取该文件。
当您重新创建文件或重新启动shell时,您获得了您在文件中看到的函数的更正版本。那个版本很管用。
这就是问题的全部,这里没有什么有趣的东西可以解释。这是一种相当常见的情况,即环境最终与设置它的文件不同步,并且经常导致暂时的混乱,但它很容易解决,就像在本例中一样。
https://unix.stackexchange.com/questions/437640
复制相似问题