4

When trying to push a couple lines to the end of a file, I get a permission issue. I understand why I'm getting the error, but I can't think of a way to resolve it. Any help would be appreciated.

sudo cat > /etc/php5/apache2/php.ini << EOF
    # extensions
    extension=”memcached.so”
    extension=”apc.so”
EOF
  • ls -al /etc/php5/apache2/php.ini, what's the permission settings of the file? – Jin Sep 26 '11 at 18:02
10

Heredoc usage, or "appending to EOF", is not the problem.

All redirections (including >) are applied before executing the actual command. In other words, your shell first tries to open /etc/php5/apache2/php.ini for writing using your account, then runs a completely useless sudo cat.

One way to get around this:

sudo bash -c "cat >> /etc/php5/apache2/php.ini" <<EOF

(You can run an interactive shell via sudo -s, or use dd or tee for writing to the file.)


On a related note, using > will overwrite the old php.ini. Use >> to append.

  • Thanks grawity. I'm having issues running the above for hidden files. I get this kind of response: cl.ly/3K2J3Q2N1h2I3N364301 – onassar Sep 26 '11 at 18:58
  • @onassar: If you open the heredoc with "EOF", you must close it with exactly the same "EOF". Not with ".EOF" as you are currently doing. – grawity Sep 26 '11 at 19:25
  • this is the code I'm pasting in: cl.ly/1w3s1p2J3U1I3C1j0q1O it's actually just being rendered improperly by my terminal. – onassar Sep 26 '11 at 19:27
  • it seems the tabs in my code (\t character) was throwing things off. replacing those with 4 spaces did the trick. thx! – onassar Sep 26 '11 at 20:14
1

To expand on the answer by @grawity, showing how to use tee:

sudo tee /etc/php5/apache2/php.ini >/dev/null <<EOF
    # extensions
    extension=”memcached.so”
    extension=”apc.so”
EOF

or use the "-a" option of tee for appending instead of overwriting.

0

sudo su and then you have a proper shell as root. Run the command in there, without sudo prefix. Afterwards, exit to return from the root shell.

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.