前端之家收集整理的这篇文章主要介绍了
sudo 与输出重定向,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
输出重定向到当前用户没有权限的文件。
用户没有某个文件的写权限,又要通过输出重定向往该文件中写入内容。结果只能是 "Permission denied"。比如当前用户为 nick,下面的命令视图查询 /root 目录下的文件并把结果写入到 /root/test.txt 文件中(注意:用户 nick 没有对 /root/test.txt 文件的写权限):
$
-al /root/ -al /root > /root/test.txt
@H_403_17@

/root/test.txt 命令不工作的原因是:输出重定向操作是由当前 shell 执行的,它(当前 shell)没有 /root/test.txt 文件的权限,所以最终失败。
解决这个问题了,下面介绍四种方式。
$
bash -c /root/test.txt
@H_403_17@

代码保存到脚本文件 test.sh 中:
#!/bin/
-al /root > /root/test.txt
@H_403_17@
$
+x test. ./test.
@H_403_17@
40433a80d3fc5516cf8396b47.png" alt="">
$
bash <<> -al /root > /root/> EOF
@H_403_17@
$
/root/test.txt | bash
@H_403_17@
后执行命令
用户再执行命令,最后 ctrl + d 退出:

重定向到文件,另一方面还可以提供一份重定向数据的副本作为后续命令的 stdin。简单的说就是把数据重定向到给定文件和屏幕上(下图来自互联网):

输出写入文件:
$
-al /root | /root/test.txt > /dev/
@H_403_17@
/dev/null 阻止 tee 把内容输出到终端:
