bash 如何从命令行将文本附加到 /etc/apt/sources.list?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/850730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I append text to /etc/apt/sources.list from the command line?
提问by Matthew
I am new to linux, and just beginning to learn bash. I am using Ubuntu 9.04, and would like to add repositories to /etc/apt/sources.list from the command line. Basically, I would like to do this:
我是 linux 新手,刚开始学习 bash。我正在使用 Ubuntu 9.04,并想从命令行将存储库添加到 /etc/apt/sources.list。基本上,我想这样做:
sudo echo "[some repository]" >> /etc/apt/sources.list
However, even when I use sudo, I get this error:
但是,即使我使用 sudo,我也会收到此错误:
bash: /etc/apt/sources.list: Permission denied
How do I avoid this error?
如何避免此错误?
采纳答案by Matthew
In Karmic, you can just use the add-apt-repository
command, at least for PPAs.
在 Karmic 中,您可以只使用该add-apt-repository
命令,至少对于 PPA。
For example:
例如:
sudo add-apt-repository ppa:docky
回答by lothar
echo "[some repository]" | sudo tee -a /etc/apt/sources.list
The teecommand is called as the superuser via sudoand the -aargument tells tee to append to the file instead of overwriting it.
该三通命令被称为通过超级用户须藤和-a参数告诉发球附加到文件,而不是覆盖它。
Your original command failed, as the IO redirection with >>will be done as the regular user, only your echo was executed with sudo.
您的原始命令失败,因为带有>>的 IO 重定向将以普通用户身份完成,只有您的 echo 是使用 sudo 执行的。
Calling a sudo subshell like
调用 sudo 子shell,例如
sudo sh -c 'echo "[some repository]" >> /etc/apt/sources.list'
works, too as pointed out by others.
也如其他人所指出的那样有效。
回答by araqnid
The shell processes ">", "<", ">>" etc itself before launching commands. So the problem is that "sudo >> /etc/foo" tries to open /etc/foo for append before gaining privileges.
在启动命令之前,shell 会自行处理“>”、“<”、“>>”等。所以问题是“sudo >> /etc/foo”在获得权限之前尝试打开/etc/foo进行追加。
One way round this is to use sudo to launch another shell to do what you want, e.g.:
解决此问题的一种方法是使用 sudo 启动另一个 shell 来执行您想要的操作,例如:
sudo sh -c 'echo "[some repository]" >> /etc/apt/sources.list'
Or alternatively:
或者:
echo "[some repository]" | sudo sh -c 'cat >> /etc/apt/sources.list'
A simpler approach may simply be to use sudo to launch an editor on the /etc/file :)
一种更简单的方法可能只是使用 sudo 在 /etc/file 上启动编辑器:)
回答by Neil Mayhew
Better to use a separate file in /etc/apt/sources.list.d
as explained in this other answer.
最好按照其他答案中的/etc/apt/sources.list.d
说明使用单独的文件。
Note that the file name MUST end in .list or it will be ignored.
请注意,文件名必须以 .list 结尾,否则将被忽略。
回答by Salil
Following works for me
以下对我有用
sudo echo "deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen" | sudo tee -a /etc/apt/sources.list.d/10gen.list
回答by Greg Hewgill
One way to solve this is to do the redirection in a subshell:
解决这个问题的一种方法是在子shell中进行重定向:
sudo sh -c 'echo "[some repository]" >> /etc/apt/sources.list'
That way, the sh
process is executed under sudo
and therefore has the necessary privileges to open the redirected output to /etc/apt/sources.list
.
这样,该sh
进程在下执行sudo
,因此具有将重定向输出打开到/etc/apt/sources.list
.
回答by kenorb
Here is solution without using piping, just simple in-place editing:
这是不使用管道的解决方案,只需简单的就地编辑:
sudo ex +'$put = \"[some repository]\"' -cwq /etc/apt/sources.list
The ex
is equivalent to vi -e
.
在ex
相当于vi -e
。
回答by Shapeshifter
If you were to login as su (if you have the privilege) the same command will work just fine...
如果您以 su 身份登录(如果您有权限),则相同的命令将可以正常工作...
su
echo "[some repository]" >> /etc/apt/sources.list
If you are not the superuser then go with Lothar's answer.
如果您不是超级用户,请使用 Lothar 的答案。
If you do it this way be sure to exit from su so that you are not running unnecessary programs as root (superuser)
如果您这样做,请务必从 su 退出,这样您就不会以 root(超级用户)身份运行不必要的程序
回答by Jihad Ismail
first open or create the file you want to edit it by the following command
首先通过以下命令打开或创建要编辑的文件
1- sudo nano file_name
2- edit the file after it opens
3- ctrl+x
4- press 'Y' to say yes
1- 须藤纳米文件名
2-打开后编辑文件
3- ctrl+x
4-按'Y'说是
and you are done.
你就完成了。