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.

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?

share|improve this question

migrated from stackoverflow.com Oct 21 '10 at 7:45

This question came from our site for professional and enthusiast programmers.

3 Answers 3

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.

share|improve this answer
    
It still gives me "Permission denied". –  8k_of_power Oct 21 '10 at 6:14
    
I have edited the answer. use sudo tee rather. –  Benoit Oct 21 '10 at 6:16

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

share|improve this answer
    
Or get persistent su-rights with just su. –  Chetan Oct 21 '10 at 6:09
1  
@Chetan: This does not work in Ubuntu. (The question is tagged with Ubuntu, so I expect that it was in this consense) –  MOnsDaR Oct 21 '10 at 6:11
    
sorry, I forgot to mention that this is in a script so I can't do sudo to run it. –  8k_of_power Oct 21 '10 at 6:13
    
If its in a script you could start the script with sudo as far as I know: # sudo ./myScript –  MOnsDaR Oct 21 '10 at 6:14
    
But since it's a script I cannot use sudo -s inside the script cause the lines after it won't run. –  8k_of_power Oct 21 '10 at 6:17

This part: sudo sed -e "s,%,$,g" >/etc/init.d/dropbox cen be viewed as this:

sudo somecommand --put the result of the sudo command into--> /etc/init.d/dropbox

In the same way as this:

ls somedirectory > filename
ls somedirectory --put the result of the ls command into--> filename

This means that the file writing will be done as your current user, not as root.

You can solve it by using tee as Benoit shows in his answer.

share|improve this answer
    
Thanks for the description of the problem. But how do I solve it? –  8k_of_power Oct 21 '10 at 6:16

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.