waldyr.ar
waldyr.ar

Reputation: 15204

How to create file and put string on it using shellscript?

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?

Upvotes: 0

Views: 354

Answers (1)

tripleee
tripleee

Reputation: 189377

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

Upvotes: 2

Related Questions