Maya Amitai Yahalom
Maya Amitai Yahalom

Reputation: 33

Add lines to /etc/hosts file

I have RHEL and i wish to run a shell script that will add several lines (over 10 lines) to the /etc/hosts file. I tried to use

sed -i "10.161.61.111 acr111" /etc/hosts 
sed -i "10.161.61.110 acr110" /etc/hosts

and so on, but i get

sed: -e expression #1, char 3: unknown command: `.'

Any idea how to fix this? Or maybe another way to run sh file which will add hosts to the hosts file? Thanks,

Upvotes: 2

Views: 9150

Answers (4)

johnnymnmonic
johnnymnmonic

Reputation: 804

You most include -i[SUFFIX], --in-place[=SUFFIX] as follow

sed -i "3i10.161.61.111 acr111" /etc/hosts

ni is the line number where the text will be appen

Upvotes: 2

Jotne
Jotne

Reputation: 41460

If data comes from a file do:

cat newdata >> /etc/hosts

If data comes from a variable:

echo "$newdata" >> /etc/host

Upvotes: 2

Cyrus
Cyrus

Reputation: 88939

Try this to append (>>) multiple lines to /etc/fstab:

cat << EOF >> /etc/fstab
10.161.61.111 acr111
10.161.61.110 acr110
10.161.61.109 acr109
10.161.61.108 acr108
EOF

Upvotes: 2

Sam
Sam

Reputation: 3490

Have you read the man page for sed? You're not using the -i parameter correctly.

Instead, why don't you just use:

echo "10.161.61.111 acr111" >> /etc/hosts
echo "10.161.61.110 acr110" >> /etc/hosts

Upvotes: 4

Related Questions