这是我的代码:
#!/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
Run Code Online (Sandbox Code Playgroud)
在第7行,它给出了错误:权限被拒绝.我已经制作了两个文件chmod 777,所以我不知道它来自哪里.
谢谢你的任何建议!
$()从du和sort行中删除它们是这样的:
sudo du -hs /home/test/* >> /home/test/hello.txt
sudo sort -n /home/test/hello.txt >> /home/test/hello1.txt
Run Code Online (Sandbox Code Playgroud)
该$()取括号内的操作的结果,并试图执行它的命令.如果此结果不是可以运行的结果,您将收到各种错误消息.
权限被拒绝即将到来,因为无论$()你的机器内部发生什么结果也恰好是你无法执行的事情.在我的脚本的测试中,我也得到Is a directory和command not found.它实际上与hello.txtor 的模式无关hello1.txt.
我应该提一下,我不确定你正在寻找的结果,所以做出上述改变可能会或可能不会得到你想要的.但是,脚本现在将运行,为您提供原始du结果hello.txt和类型的排序结果hello1.txt.你可能会需要调试不大不小的一点,如果你想从最小的磁盘使用情况,最大的一个列表(提示:尝试过离开-h从du).