Young's Blog

包子铺

Sudo执行命令的重定向处理

在编译portsnap的时候,不想修改Makefile,想直接把编译的语句保存到sh里,然后修改优化选项再得到带调试信息的portsnap,结果运行下面的这个命令时:

sudo make >1.sh 2>&1

返回的结果提示为:

1.sh: Permission denied.

仔细考虑了一下,这里应该是sudo 把make先作为执行的命令,然后>1.sh 2>&1才被sudo执行完后的shell来执行。此时已经失去了对应的root权限。所以会提示Permission Denied。解决方法为:

sudo sh -c 'make >1.sh 2>&1'

此时Sudo使用sh -c重新开启了一个SubShell,由SubShell来执行相应的重定向操作。在sudo的帮助里的最后一个例子里,给出来上述的使用方法:

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"

posted on 2013-05-05 22:46 Young 阅读(3386) 评论(0)  编辑 收藏 引用 所属分类: FreeBSD


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理