LinuxGem
此处为老的 LinuxGem,新版 LinuxGem 请移步 www.linuxgem.org

echo 与 sudo

galeki posted @ 2009年4月23日 05:36 in Bash 和它的朋友们 with tags echo sudo , 5984 阅读

众所周知,使用 echo 并配合命令重定向是实现向文件中写入信息的快捷方式。本文介绍如何将 echo 命令与 sudo 命令配合使用,实现向那些只有系统管理员才有权限操作的文件中写入信息。

比如要向 test.asc 文件中随便写入点内容,可以:

$ echo "信息" > test.asc
# 或者
$ echo "信息" >> test.asc

下面,如果将 test.asc 权限设置为只有 root 用户才有权限进行写操作:

$ sudo chown root.root test.asc

然后,我们使用 sudo 并配合 echo 命令再次向修改权限之后的 test.asc 文件中写入信息: 

$ sudo echo "又一行信息" >> test.asc
-bash: test.asc: Permission denied

这时,可以看到 bash 拒绝这么做,说是权限不够。这是因为重定向符号 “>” 和 ">>" 也是 bash 的命令。我们使用 sudo 只是让 echo 命令具有了 root 权限,但是没有让 “>” 和 ">>" 命令也具有 root 权限,所以 bash 会认为这两个命令都没有像 test.asc 文件写入信息的权限。

解决这一问题的途径有两种。第一种是利用 "sh -c" 命令,它可以让 bash 将一个字串作为完整的命令来执行,这样就可以将 sudo 的影响范围扩展到整条命令。具体用法如下: 

$ sudo sh -c 'echo "又一行信息" >> test.asc'

另一种方法是利用管道和 tee 命令,该命令可以从标准输入中读入信息并将其写入标准输出或文件中,具体用法如下:

$ echo "第三条信息" | sudo tee -a test.asc

注意,tee 命令的 "-a" 选项的作用等同于 ">>" 命令,如果去除该选项,那么 tee 命令的作用就等同于 ">" 命令。

  • 无匹配
coolwanglu 说:
2009年4月23日 07:52

有道理!
我都是

sudo bash
command
exit

zigzed 说:
2009年4月23日 18:45

不错,这个介绍无论内容和风格都很好。

ptubuntu 说:
2009年4月25日 07:04

这个主题有些不太对吧.这个只能说明sudo的使用权限方式.

Head_small
galeki 说:
2009年4月25日 07:14

只是想通过一个具体的问题来揭示使用 sudo 时的注意事项。以后,有关 bash 的大部分文章,都可以通过具体的例子开展。

zyshipin 说:
2009年6月11日 18:47

最后的方法比较不错

BLOOD PRESSURE READI 说:
2021年1月28日 04:21

Advantageously, typically the submit is really the very best about this laudable theme. To be sure with all your a conclusion and will thirstily await the following revisions. Really stating cheers won’t only end up being suitable, to your good readability within your creating. I may at once seize a person’s rss to sleep in abreast of virtually any upgrades. Fine job and much success within your organization business!

Ross Levinsohn Maven 说:
2021年1月30日 20:37

Advantageously, typically the submit is really the very best about this laudable theme. To be sure with all your a conclusion and will thirstily await the following revisions. Really stating cheers won’t only end up being suitable, to your good readability within your creating. I may at once seize a person’s rss to sleep in abreast of virtually any upgrades. Fine job and much success within your organization business!

fdfd 说:
2021年3月12日 23:13

Pleasant site and completely extraordinary. You can improve yet regardless I say this perfect.Keep striving generally advantageous.  Buy 5 MeO DMT

amelia 说:
2021年3月14日 23:07

i was simply perusing along and happened upon your web journal. simply needed to say great online journal and this article truly helped me.  home accessories wholesale

amelia 说:
2021年3月18日 05:34

I appreciate what you have done here. I like the part where you say you are doing this to give back yet I would expect by every one of the remarks this is working for you too.  wholesale mens shoes

amelia 说:
2021年3月18日 05:34

Pleasant to peruse your article! I am anticipating sharing your enterprises and encounters.  wholesale silver j

ch 说:
2022年1月25日 04:15

A debt of gratitude is in order for setting aside an ideal opportunity to examine that, I feel unequivocally about this thus truly like becoming acquainted with additional on this sort of field. Do you psyche overhauling your blog entry with extra understanding? It ought to be truly helpful for every one of us. https://www.5g999.co/slot

ch 说:
2022年2月08日 19:58

There is such a great amount in this article I would never have considered all alone. Your substance gives perusers things to consider in a fascinating way. Much obliged to you for your reasonable data. facial salon equipment

ch 说:
2022年2月10日 05:15

Much obliged to you for extremely usefull data.. Bluze

ch 说:
2022年2月14日 03:50

pleasant bLog! its fascinating. much thanks to you for sharing.... https://usdhnfu.com

ch 说:
2022年2月14日 03:50

I’ve read some good stuff here. Definitely worth bookmarking for revisiting. I surprise how much effort you put to create such a great informative website. http://www.ruled8tode.com

ch 说:
2022年2月14日 03:50

Thanks for your post. I’ve been thinking about writing a very comparable post over the last couple of weeks, I’ll probably keep it short and sweet and link to this instead if thats cool. Thanks. http://www.betflix898.com

ch 说:
2022年2月14日 03:50

Pleasant post. I was checking always this web journal and I'm inspired! To a great degree valuable data extraordinarily the last part I tend to such data a ton. I was looking for this sure data for quite a while. Much obliged to you and good fortunes. www.betflixgt.com

ch 说:
2022年3月18日 05:55

Positive site, where did u concoct the data on this posting? I'm satisfied I found it however, sick be returning soon to figure out what extra posts you incorporate. Heavy-R

SEO 说:
2022年3月20日 06:23

Is anyone in need of an adhesive vinyl? https://www.digitekprinting.com/ this website and order yours now. Their custom printed stickers can come in various forms and their staff are there to help you. I really loved working with them.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter