1

As part of a post-preseeding script, I want to disable ipv6.

In /etc/default/grub I want to change this line:

GRUB_CMDLINE_LINX=""

so that it reads:

GRUB_CMDLINE_LINUX="ipv6.disable=1"

Which can be done with:

sed -i "s/GRUB_CMDLINE_LINUX=\"\"/GRUB_CMDLINE_LINUX=\"ipv6.disable=1\"/" /etc/default/grub

But sed won't match if GRUB_CMDLINE_LINUX already has arguments in it.

How can I add the parameter while preserving any existing arguments (if any) ?

3
  • So if empty, fill with "ipv6...". Otherwise, keep the value? Commented Jul 15, 2013 at 15:14
  • Maybe you should try GRUB_CMDLINE_LINUX instead of GRUB_CMD_LINE_LINUX (delete the underscore between CMD and LINE). Commented Jul 15, 2013 at 15:15
  • thanks uzolt, just a typo. @fedorqui, no, if there's a value already there, keep it and add ipv6.disable=1 inside the quotes, if there's no value, just add it. Commented Jul 15, 2013 at 15:17

1 Answer 1

3

The problem is that you are nuking the entire line. Try

s/GRUB_CMD_LINE_LINUX=\"/GRUB_CMD_LINE_LINUX=\"ipv6.disable=1XXX/

to insert your assignment to the front and replace XXX with the character used as a separator. Perhaps ;. Alternatively

s/(GRUB_CMD_LINE_LINUX=[^\"]*)\"$/\1XXXipv6.disable=1"/

to insert at the end of the line. ie after all existing params. You may have to escape the parenthesis and/or " in the second version.

Solved with:

sed "s/GRUB_CMDLINE_LINUX=\"\(.*\)\"/GRUB_CMDLINE_LINUX=\"\1 ipv6.disable=1\"/" /etc/default/grub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.