权限被拒绝,尽管文件已经chmod 777

4

这是我的代码:

#!/bin/sh
sudo touch /home/test/hello.txt
sudo chmod 777 /home/test/hello.txt
sudo touch /home/test/hello1.txt
sudo chmod 777 /home/test/hello1.txt
"$(sudo du -hs /home/test/*)" >> /home/test/hello.txt
"$(sudo sort -n /home/test/hello.txt)" >> /home/test/hello1.txt
head -3 /home/test/hello1.txt

在第7行报错:权限被拒绝。我已经把这两个文件的chmod设置为777,所以我不知道这是从哪里来的。

感谢任何建议!

1个回答

6

dusort行中删除$(),使它们变成这样:

sudo du -hs /home/test/* >> /home/test/hello.txt
sudo sort -n /home/test/hello.txt >> /home/test/hello1.txt
$()会将括号内的操作结果作为命令执行。如果结果无法运行,则会出现各种错误消息。出现“Permission denied”是因为在您的计算机上,$()内部的结果也恰好是您无法执行的内容。在我测试您的脚本时,我还遇到了“Is a directory”和“command not found”。这与hello.txthello1.txt的模式无关。

需要说明的是,我不确定您要寻找的结果是什么,所以进行以上更改可能会得到您想要的结果,也可能不会。但是,脚本现在可以运行,在hello.txt中提供原始的du结果,在hello1.txt中提供了某种程度的排序结果。如果您试图从最小磁盘使用量到最大磁盘使用量获取列表(提示:尝试省略du中的-h),则可能需要对排序进行一些调试。


非常感谢!正是我在寻找的内容! - 1GDST

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接