sudo permission denied

I ran this code:

sudo cat <<EOF | sudo sed -e "s,%,$,g" >/etc/init.d/dropbox
  echo "Hello World"
EOF

But even though, I get "permission denied", cause you have to be root to make changes in the /etc/init.d directory. And somehow, my command above doesn't work.

Is there a way to solve this?


The redirection to a file is handled by bash. It does therefore not inherit permissions granted by sudo.

Use sudo tee for writing to a file as root.

Try this:

cat | sed -e 's,%,$,g' | sudo tee /etc/init.d/dropbox << EOF
  echo "Hello World"
EOF

Notice that $, inside double quotes might be interpreted.


You could grant yourself persistent su-rights with
# sudo -s

then your command (do not need to sudo anymore) and exit with
# exit

EDIT:
I assume you're asking Ubuntu-related because your question is tagged with that. In other distribution like Suse you'll have the ability to use
# su
instead of # sudo -s