Des*_*ume 43 linux bash shell command-line sudo
当我尝试使用sudo将文件中的数据附加到另一个文件时,这是导致"权限被拒绝"的shell命令:
sudo cat add_file >> /etc/file
Run Code Online (Sandbox Code Playgroud)
文件at /etc/file由root(即我)拥有,其权限是rw-r--r--.我应该root暂时让它成功还是有解决方法sudo?
Chr*_*our 64
运行bash方式sudo:
$ sudo bash -c "cat add_file >> /etc/file"
$ whoami;sudo bash -c "whoami";whoami
iiSeymour
root
iiSeymour
Run Code Online (Sandbox Code Playgroud)
Gil*_*not 26
尝试这样做:
sudo tee -a /etc/file < add_file
Run Code Online (Sandbox Code Playgroud)
它比运行bash还要轻 sh -c command
一个有趣的可能性是使用ed(标准编辑器):
sudo ed -s /etc/file <<< $'r add_file\nwq'
Run Code Online (Sandbox Code Playgroud)
我知道我不会因为这个美妙的答案而被投票,但我想把它包含在这里(因为它很有趣).完成!
您正在尝试将命令的结果写入sudo cat add_file文件/ etc/file.显然你没有这个权利.
man sudo 举个例子:
Run Code Online (Sandbox Code Playgroud)To make a usage listing of the directories in the /home partition. Note that this runs the commands in a sub-shell to make the cd and file redirection work. $ sudo sh -c "cd /home ; du -s * | sort -rn > USAGE"
所以你应该尝试:
sudo sh -c "cat add_file >> /etc/file"
| 归档时间: |
|
| 查看次数: |
26878 次 |
| 最近记录: |