1

I am experiencing a strange behavior using a sed command (got to be sed, no awk in this case) and got two questions for you, hoping someone got the skills to explain me what I am doing wrong.

  • I want to append a comment and two commands to /etc/sudoers using the following one-liner:

    sed -i -e '$a\# Provide apache user permissions to run the ban_ip.sh script as part of mod_evasive\n\apache ALL=NOPASSWD: /usr/local/bin/scripts-tecmint/ban_ip.sh\n\Defaults:apache !requiretty' /etc/sudoers
    

    Strage things happening here:

    • The first line (a comment) gets appended at the end of the file directly without an empty line created (just a "design thing" I´d linke to fix)
    • The second line created starts now with ^Gpache instead of apache

Questions:
1. How can I append an emtpy line using my sed one-liner to the end of the file and then having my text inserted after that empty line?
2. What do I need to fix in my one-liner to have sed adding the text I want (2nd line to start with apache and not ^Gpache

Your input is much appreciated.

2

Is there some particular reason to use sed? Why not ...

echo -e '\n# Provide apache user permissions to run the ban_ip.sh script as part of mod_evasive\napache ALL=NOPASSWD: /usr/local/bin/scripts-tecmint/ban_ip.sh\nDefaults:apache !requiretty' >>/etc/sudoers

The first 'a' of 'apache' is being escaped by the '\' right before it, resulting in your '^Gpache'. You should be able to resolve that particular situation by simply removing the '\' after your '\n'. Starting your comment on a new line could probably be accomplished by inserting a '\n' after your '$a\'. Maybe something like...

sed -i -e '$a\ \n# Provide apache user permissions to run the ban_ip.sh script as part of mod_evasive\napache ALL=NOPASSWD: /usr/local/bin/scripts-tecmint/ban_ip.sh\nDefaults:apache !requiretty' /etc/sudoers

Full Disclosure: I haven't tested either of these solutions.

  • Plain echo does not translate escape sequences: use echo -e or printf – glenn jackman Apr 2 '15 at 19:46
  • @glennjackman that's why I included the '-e' switch – smokes2345 Apr 3 '15 at 20:45
  • 2nd options works for me. Thanks 'smokes*' – frank Apr 4 '15 at 21:28
3

Because the \a character is Ctrl+G -- you don't need to escape the character after \n

For maintainability, I'd recommend slightly reducing the one-liner-ness of it, and use actual newlines to continue the a command. This also enables the blank line you want.

sed -i -e '$a\
\
# Provide apache user permissions to run the ban_ip.sh script as part of mod_evasive\
apache ALL=NOPASSWD: /usr/local/bin/scripts-tecmint/ban_ip.sh\
Defaults:apache !requiretty' /etc/sudoers
  • Hi glenn, thank you for explaining this to me. While you got the right solution, I am using a yaml script and prefer onliners (bad experience in the past with multi line scripts). – frank Apr 4 '15 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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