petelam
petelam

Reputation: 43

Check if files exists in a directory

I want to check if some files exist in a directory. If yes, I want to skip over those files and not perform any procedure. If no, then I want to perform some procedure.

Let say in directory /setup/server I have the following files:

systems-server01
systems-server02
system-server03
system-server04

If I run the command ./add-system.sh system-server05 my code should execute the readline procedure and add a new server.

But if I run the command ./add-system.sh system-server04 my code should echo "Server already exists, please enter a new server".

Here is what I have so far

#!/bin/bash
DOMAIN=my.home.fs.cville.com
if [ $# -lt 1]; then
   echo "Please enter filename\n"
   exit
fi

#I think this is where I need to do the check if files exist but I have 
#problem figure out how to do it

while read line ; do
    alt=(${line[@]}
    hostname=${alt[0]}
    ipaddress=${$alt[1]}
    mac=${alt[2]}
    iface=${alt[3]}
    profile=${alt[4]}

    copper system add --cobber --name=$hostname --profile=$profile --ip- 
    address=$ipaddress \ --interface=$iface --mac=$mac 
    --hostname=$hostname --dns-name=${hostname}.$DOMAIN
done<$1
copper sync

Upvotes: 0

Views: 77

Answers (1)

stellarhopper
stellarhopper

Reputation: 670

Yes where your comment block is, you can add a check like:

if [ -e /setup/server/"$1" ]; then
    echo "file exists, nothing to do"
    exit
fi

See 'help test' for the various tests available. '[' is synonymous to 'test'

Upvotes: 2

Related Questions