Bash Deployment Script Permission Problem

2019-03-03 00:16发布

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?

2条回答
Juvenile、少年°
2楼-- · 2019-03-03 00:53

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
查看更多
姐就是有狂的资本
3楼-- · 2019-03-03 01:17

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

查看更多
登录 后发表回答