我正在尝试将一些文本推送到需要sudo以下内容的文件中:
sudo echo "some text" > /etc/path/to/file
Run Code Online (Sandbox Code Playgroud)
但我不断收到以下错误:
bash: /etc/path/to/file: Permission denied
Run Code Online (Sandbox Code Playgroud)
我可以用 来做nano,但我需要以编程方式来做。是>运营商的问题吗?有我可以使用的脚本吗?
Shell(bash在本例中)>在运行命令之前首先执行重定向 ( )。So/etc/path/to/file将在使用sudoeven 运行的命令之前创建,并且普通用户没有足够的权限来创建文件/etc/path/to/file,因此会出现有关权限的错误消息。
你可以做:
sudo bash -c 'echo "some text" > /etc/path/to/file'
Run Code Online (Sandbox Code Playgroud)
或使用tee:
echo "some text" | sudo tee /etc/path/to/file
Run Code Online (Sandbox Code Playgroud)