0

I want to create a file in /usr/share/applications/ and put a string on it.

What I have so far:

sudo touch /usr/share/applications/test.desktop
dentry="testing"
sudo echo $dentry >> /usr/share/applications/test.desktop

But this raise an error Permission Denied. What should I do to make it works?

3
  • you need to determine which sudo is causing the error message. Copy/paste 1 line at a time to the cmdline and execute it. See where the error is. I bet it is the first line. Are you sure you're setup properly to sudo commands. That is a whole chapter in a book ;-) . good luck. Commented Aug 9, 2012 at 15:01
  • First and second lines are ok! The problem can be third. Commented Aug 9, 2012 at 15:05
  • 1
    As an aside, you should always double-quote your variable interpolations. echo "$dentry" will preserve whitespace, line breaks, and shell metacharactersbin the variable's value. Commented Aug 9, 2012 at 15:46

1 Answer 1

2

You should create the file using your own pernissions, then sudo cp it into place.

The reason the second command doesn't work is that the redirection is set up by your shell, before sudo even runs. You could work around this by running sudo sh -c 'echo stuff >>file' but this is vastly more risk-prone than a simple sudo cp, and additionally has a race condition (if you run two concurrent instances of this script, they could end up writing the information twice to the file).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.