1

I'm trying to create a script where if I file exists then perform a delete and then get a wget from artifactory. Running this code below for some reason will not delete my file and creates a new one with an extension ...example cageo.crt.1 , .2, .3, etc.

When I try to debug it will show rm -f but without the file name

#!/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

This is my output:

+ 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’
codeforester
  • 39,467
  • 16
  • 112
  • 140
Nabeel A.Rahman
  • 181
  • 3
  • 14
  • 2
    Remove the space between `file`, `=` and `cageo.crt`. It should read `file=cageo.crt` – iamauser Oct 19 '17 at 21:51
  • iamauser is right ... also make sure you are not using an alias for rm. Use the fully qualified path ... something like `/bin/rm`. – Red Cricket Oct 19 '17 at 21:53
  • You can move the wget command out of the if-then-else-fi block – Walter A Oct 19 '17 at 21:58
  • Here's a bunch of examples of using `wget` to fetch sources and overwrite them with the latest: [Noloader | Build-Scripts](https://github.com/noloader/Build-Scripts). The scripts don't worry about deleting the old old file. They use `wget` and the `-O` option to overwrite an existing file. – jww Oct 19 '17 at 23:40

1 Answers1

3

Remove the space when assigning variables in shell. It should read,

file="cageo.crt"

And then use,

if [ -f "$file" ]; 

See this SO question: https://unix.stackexchange.com/questions/258727/spaces-in-variable-assignments-in-shell-scripts

iamauser
  • 11,119
  • 5
  • 34
  • 52