How to dynamically insert a DNS in / etc / hosts through a bash script?

1

I have a bash runmydocker.sh script whose purpose is to retrieve the string sent by the user when he is calling that script and create a DNS in etc/hosts . Example:

The user (developer) will call runmydocker.sh mysitephp7.com . This script will retrieve the string mysitephp7.com and insert it into /etc/hosts as 127.0.0.1 mysitephp7.com and then initialize docker. Then when the developer writes in the browser url mysitephp7.com the system would be loaded using the docker server. Note that this DNS did not exist on the hosts before the script.

All steps are ready except for inserting this string mysitephp7.com .com into / etc / hosts.

What would be the proper way to do this?

    
asked by anonymous 20.11.2017 / 21:55

1 answer

1

Inside your script runmydocker.sh we will basically have the following:

#!/bin/bash

HOST=$1
# Atribui o primeiro parâmetro passado no script a variável HOST

echo "127.0.0.1 $HOST" >> /etc/hosts
# Adiciona a saida "127.0.0.1 $HOST" ao final do arquivo /etc/hosts 

The > > redirector inserts the output of your command at the end of the file. More about redirectors click here .

    
21.11.2017 / 17:47