【发布时间】:2017-10-19 21:48:55
【问题描述】:
我正在尝试创建一个脚本,如果我的文件存在,则执行删除,然后从 artifactory 获取wget。出于某种原因在下面运行此代码不会删除我的文件并创建一个带有扩展名的新文件...例如 cageo.crt.1、.2、.3 等。
当我尝试调试时,它会显示 rm -f 但没有文件名
#!/bin/bash -ex
#install Certificate
cd /deployment/scripts
# check if cert is already installed
file = cageo.crt
if [ -f $file ]; then
echo "File $file exists."
echo "Delete file and get latest"
rm -f $file
wget https://artifactory.geo.com/artifactory/cageo.crt
else
wget https://artifactory.geo.com/artifactory/cageo.crt
fi
这是我的输出:
+ cd /deployment/scripts
+ file = cageo.crt
=: cannot open (No such file or directory)
cageo.crt: PEM certificate
+ '[' -f ']'
+ echo 'File exists.'
File exists.
+ echo 'Delete file and get latest'
Delete file and get latest
+ rm -f
+ wgethttps://artifactory.geo.com/artifactory/cageo.crt/cageo.crt
--2017-10-19 17:39:16-- https://artifactory.geo.com/artifactory/cageo.crt/cageo.crt
Resolving artifactory.geo.com (artifactory.geo.com)... 10.0.138.51
Connecting to artifactory.geo.com (artifactory.geo.com)|10.0.138.51|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1675 (1.6K) [application/octet-stream]
Saving to: ‘cageo.crt.4’
【问题讨论】:
-
删除
file、=和cageo.crt之间的空格。它应该是file=cageo.crt -
iamauser 是对的……还要确保您没有使用 rm 的别名。使用完全限定的路径...类似于
/bin/rm。 -
您可以将 wget 命令移出 if-then-else-fi 块
-
这里有一堆使用
wget获取源并用最新的覆盖它们的示例:Noloader | Build-Scripts。脚本不用担心删除旧的旧文件。他们使用wget和-O选项覆盖现有文件。