Skip to content
정명주(myeongju.jung) edited this page Feb 7, 2018 · 1 revision

hosts.sh

#!/usr/bin/env bash

WHO=`whoami`
if [ "root" != "$WHO" ]
then
    echo "You are necessary root account."
    exit 2
fi

if [ -z $1 ] || [ -z $2 ]
then
    echo "./hosts.sh ipaddress hostname"
    exit 2
fi

IP=$1
HOST=$2

if grep -F ${2} /etc/hosts
then
    echo "Already exists $HOST. Will change"
    sed -i "/$HOST/ s/.*/$IP $HOST/g" /etc/hosts
else
    echo "Not exists $HOST. Will add"
    echo "$IP $HOST" >> /etc/hosts
fi

test-hosts.sh

#!/usr/bin/env bash

WHO=`whoami`
if [ "root" != "$WHO" ]
then
    echo "You are necessary root account."
    exit 2
fi

./hosts.sh 0.0.0.1 domain1.com
./hosts.sh 0.0.0.2 domain2.com
./hosts.sh 0.0.0.3 domain3.com

if [ "$?" -gt "0" ]
then
    # Do work for when command has a failure exit
    exit $?
fi
Clone this wiki locally