-1

Is there a way to run a if command in a script that can match multiple values for example I would push a script to a bunch of servers and each server would only run the command if the servers host name is one of the entered values in the command or list.

I tried something like this but it obviously didn't work..

#!/bin/bash
if [ $HOSTNAME = server1.domain.com, server2.domain.com, server3.domain.com, server4.domain.com ]
then
        /home/user/update_1
else
        /home/user/update_2
fi

It would be even better if I could just have it check against a file with a list of servers

1
  • 1
    Stop this and go look into a real configuration management system. Ansible, salt, puppet, chef, whatever. Mar 19, 2015 at 18:55

1 Answer 1

0

You need to use the || or syntax for this

if [ "$HOSTNAME" == 'server1.domain.com' ] || [ "$HOSTNAME == 'server2.domain.com'"]
then
    do something
else
    dont' do something
fi

if you want to check against a list of files then you could do something like this:

echo -e "computer1\ncomputer2\nmycomputer" > computerfile
hostname=mycomputer
if grep "$hostname" computerfile > /dev/null
then
    echo true
else
    echo false
fi

where hostname is the name of the variable containing the computer name and computerfile is a list of computers (1 name per line).

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .