2017-10-19 270 views
0

我想創建一個腳本,如果我的文件存在然後執行刪除,然後從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’ 
+2

刪除'file','='和'cageo.crt'之間的空間。它應該讀取'file = cageo.crt' – iamauser

+0

iamauser是正確的...還要確保你沒有使用rm的別名。使用完全合格的路徑...就像'/ bin/rm'。 –

+0

您可以將wget命令移出if-then-else-fi塊 –

回答

相關問題