简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM