I'm writing a script which will automatically configure a NIS client with the correct settings. I'm trying to set the /etc/nsswitch.conf file and I'd like to overwrite whatever is already there with my settings. Now my question is, how can I do that? how can i paste about 20 lines of settings into the client's nsswitch.conf file straight from the script? I know i can do something like:
echo "line 1" > /etc/nsswitch.conf
echo "line 2" >> /etc/nsswitch.conf
But that's an ugly way to do it, I hope there is a better way to achieve this goal
I would use cat
together with here-doc syntax for this:
cat <<EOF > /etc/nsswitch.conf
group: compat
shadow: compat
hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4
...
EOF
The statement above will overwrite or create the file with the contents between the first line and EOF
. In the form above even variables like group: $group
would be expanded by bash. If you don't want this, then use <<'EOF'
(note the single quotes '
around the EOF
)