Score:1

Check a host file for modification and if missing add it

cn flag

I'm using docker in which the container has its own /etc/hosts file. It gets cleared out when rebooting. I'd like to create a script to check for an entry and if missing add it.

So if /etc/hosts has an entry that says:

10.1.1.1 important-server

Then do nothing. If not do:

echo "10.1.1.1 important-server" >> /etc/hosts

I'm assuming I can use sed or awk or something like that for the pattern matching.

cn flag
You should add `--add-host="import-server:10.1.1.1"` to the docker run command OR docker-compose.yml file if you use docker-compose.
Raffa avatar
jp flag
`grep` with the option `-q` will `exit 0`(*success*) at first match and will `exit > 0` otherwise ... So, ... `/bin/grep -Fq '10.1.1.1 important-server' /etc/hosts || echo "10.1.1.1 important-server" >> /etc/hosts`
Gilles Quenot avatar
tn flag
Already gave a response of this earlier ;) And the spaces are not fixed like this in **RealLife©®™**
Mark Vetter avatar
cn flag
--add-host "import-server:10.1.1.1" works. It should not have the =
Gilles Quenot avatar
tn flag
Advice to newcomers: If an answer solves your problem, please accept it by clicking the large check mark (✓) next to it and optionally also up-vote it (up-voting requires at least 15 reputation points). If you found other answers helpful, please up-vote them. Accepting and up-voting helps future readers.
Score:1
tn flag

As said by Rinzwind in comments:

You should add --add-host="import-server:10.1.1.1" to the docker run command OR docker-compose.yml file if you use docker-compose.

Or in shell:

sudo -s
grep -Pq '^10\.1\.1\.1\s+important-server' /etc/hosts ||
    echo '10.1.1.1 important-server' >> /etc/hosts
exit
Mark Vetter avatar
cn flag
Correct except change = to :
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.