Bash Deployment Script Permission Problem

a 夏天 提交于 2019-12-31 04:54:32

问题


I'm writing a deployment script and have run in to a strange problem...

ian@baster:~$ sudo echo "Build: "$REVISION" - Deployed: "$(date +%Y-%m-%d) > /home/www/prod/www/revision.html
-bash: /home/www/prod/www/revision.html: Permission denied

but...

root@baster:~# echo "Build: "$REVISION" - Deployed: "$(date +%Y-%m-%d) > /home/www/prod/www/revision.html
root@baster:~# more /home/www/prod/www/revision.html
Build:  - Deployed: 2011-01-28

then...

ian@baster:~$ sudo ls -l /home/www/prod/www
total 28
-rw-r--r-- 1 root     root       31 2011-01-28 21:56 revision.html

ian@baster:~$ sudo more /home/www/prod/www/revision.html
Build:  - Deployed: 2011-01-28

What's the deal?


回答1:


The echo is run as root, but not the redirection. Run the redirection in a sudo subshell.




回答2:


The usual way to do that is with tee:

echo "foo" | sudo tee filename

You can suppress the output to the screen which tee does like this:

echo "foo" | sudo tee filename >/dev/null


来源:https://stackoverflow.com/questions/4834896/bash-deployment-script-permission-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!