6

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
studiohack
  • 13,468
  • 19
  • 88
  • 118
onassar
  • 173
  • 1
  • 1
  • 5
  • `ls -al /etc/php5/apache2/php.ini`, what's the permission settings of the file? – Jin Sep 26 '11 at 18:02

3 Answers3

15

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.

u1686_grawity
  • 426,297
  • 64
  • 894
  • 966
  • Thanks grawity. I'm having issues running the above for hidden files. I get this kind of response: http://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. – u1686_grawity Sep 26 '11 at 19:25
  • this is the code I'm pasting in: http://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
5

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.

Andreas Maier
  • 171
  • 2
  • 5
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.

Daniel Beck
  • 109,300
  • 14
  • 287
  • 334