3

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

1 Answer 1

7

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)

1
  • For reference, the ... in the above example is illustrative only and not part of the syntax.
    – StudioLE
    Commented Nov 26, 2018 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.