How to dynamically insert a DNS into /etc/hosts via a bash script?

Asked

Viewed 103 times

1

I have a bash script runmydocker.sh whose goal is to retrieve the string sent by the user at the time he is calling this script and create a DNS on etc/hosts. Example:

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

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

what would be the proper way to achieve this?

1 answer

1


Within 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.