0

What am I doing wrong with this sample code?

for HOSTNAME in $(cat hostnames.txt)
do
        file ="/path/$HOSTNAME.crt"
        if [[-e "$file"]]
        then
                echo "file exist"
        else
                echo "file dose NOT Exist"
        fi
done

ERROR:

=/path/ad.tc.crt: cannot open ‘=/path/ad.tc.crt’ (No such file or directory)
./certs_check.sh: line 15: [[-e: command not found
file dose NOT Exist
=/path/cpscom.ds.crt: cannot open `=/path/cpscom.ds.crt' (No such file or directory)
./certs_check.sh: line 15: [[-e: command not found
file dose NOT Exist

The first file /path/ad.tc.crt exists so it should print "file does exist".

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • Square brackets must have spaces around them. See the linked question. [Shell Check](https://shellcheck.net) is good at diagnosing errors like this when bash's diagnostics don't suffice. – John Kugelman Dec 15 '21 at 17:56
  • Also, you can't have a space before the `=` in an assignment. Spaces are important delimiters in shell syntax, so don't add or remove them unless you specifically know it's safe to. – Gordon Davisson Dec 15 '21 at 18:25

0 Answers0