我已经在我们开发的一个RedHat linux机器上获得了sudo访问权,我似乎发现自己经常需要将输出重定向到一个我通常没有写访问权的位置。
问题是,这个人为的例子不起作用:
sudo ls -hal /root/ > /root/test.out
我刚刚收到回复:
-bash: /root/test.out: Permission denied
我怎样才能让它工作呢?
我已经在我们开发的一个RedHat linux机器上获得了sudo访问权,我似乎发现自己经常需要将输出重定向到一个我通常没有写访问权的位置。
问题是,这个人为的例子不起作用:
sudo ls -hal /root/ > /root/test.out
我刚刚收到回复:
-bash: /root/test.out: Permission denied
我怎样才能让它工作呢?
当前回答
这是基于tee的答案。为了让事情更简单,我写了一个小脚本(我叫它suwrite),并把它放在/usr/local/bin/ +x权限:
#! /bin/sh
if [ $# = 0 ] ; then
echo "USAGE: <command writing to stdout> | suwrite [-a] <output file 1> ..." >&2
exit 1
fi
for arg in "$@" ; do
if [ ${arg#/dev/} != ${arg} ] ; then
echo "Found dangerous argument ‘$arg’. Will exit."
exit 2
fi
done
sudo tee "$@" > /dev/null
如代码中的USAGE所示,您所要做的就是将输出输出输送到这个脚本,后面跟着所需的超级用户可访问的文件名,如果需要的话,它将自动提示您输入密码(因为它包含sudo)。
echo test | suwrite /root/test.txt
注意,由于这是tee的一个简单包装器,它也将接受tee的-a选项来追加,并且还支持同时写入多个文件。
echo test2 | suwrite -a /root/test.txt
echo test-multi | suwrite /root/test-a.txt /root/test-b.txt
它也有一些简单的保护,防止写入/dev/设备,这是该页的一个评论中提到的一个问题。
其他回答
每当我要做这样的事情时,我就变成root:
# sudo -s
# ls -hal /root/ > /root/test.out
# exit
这可能不是最好的方法,但确实有效。
并不是要白费口功,但是这里有太多的答案使用tee,这意味着您必须将stdout重定向到/dev/null,除非您想在屏幕上看到一个副本。
一个更简单的解决方案是像这样使用cat:
sudo ls -hal /root/ | sudo bash -c "cat > /root/test.out"
请注意重定向是如何放在引号内的,以便由sudo启动的shell而不是运行它的shell来计算它。
sudo at now
at> echo test > /tmp/test.out
at> <EOT>
job 1 at Thu Sep 21 10:49:00 2017
您的命令不起作用,因为重定向是由您的shell执行的,shell没有写入/root/test.out的权限。输出的重定向不是由sudo执行的。
有多种解决方案:
Run a shell with sudo and give the command to it by using the -c option: sudo sh -c 'ls -hal /root/ > /root/test.out' Create a script with your commands and run that script with sudo: #!/bin/sh ls -hal /root/ > /root/test.out Run sudo ls.sh. See Steve Bennett's answer if you don't want to create a temporary file. Launch a shell with sudo -s then run your commands: [nobody@so]$ sudo -s [root@so]# ls -hal /root/ > /root/test.out [root@so]# ^D [nobody@so]$ Use sudo tee (if you have to escape a lot when using the -c option): sudo ls -hal /root/ | sudo tee /root/test.out > /dev/null The redirect to /dev/null is needed to stop tee from outputting to the screen. To append instead of overwriting the output file (>>), use tee -a or tee --append (the last one is specific to GNU coreutils).
感谢Jd, Adam J. Forster和Johnathan的第二,第三和第四个解决方案。
我对这个问题的看法是:
如果您需要写入/替换文件:
echo "some text" | sudo tee /path/to/file
如果你需要追加到文件:
echo "some text" | sudo tee -a /path/to/file