2017-10-19 5 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'の間のスペースを削除します。それは

if [ -f "$file" ]; 

このSOの質問を参照してください

file="cageo.crt" 

そして使用し、お読みください。 'file = cageo.crt'を読むべきです – iamauser

+0

iamauserは正しいです...また、rmのエイリアスを使用していないことを確認してください。 '/ bin/rm'のような完全なパスを使ってください。 –

+0

wgetコマンドをif-then-else-fiブロックから移動することができます –

答えて

関連する問題