Wednesday 19 December 2018

cat - Bash "Permission denied" issue when trying to append to EOF


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

Answer



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" <

(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.


No comments:

Post a Comment

Where does Skype save my contact&#39;s avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...