Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
share|improve this question
    
ls -al /etc/php5/apache2/php.ini, what's the permission settings of the file? –  Jin Sep 26 '11 at 18:02

2 Answers 2

up vote 7 down vote accepted

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.

share|improve this answer
    
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

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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