尝试修复全局可写文件并获取以下错误:
chmod: missing operand after a+t
这是我的代码
df --local -P | awk {'if (NR!=1) print $6'} | xargs -I '{}' find '{}' -xdev -type d -perm -0002 -a ! -perm -1000 2>/dev/null | xargs chmod a+t
有什么想法吗?
答案 0 :(得分:3)
这里根本不需要xargs
- 它是你的bug的来源,如果你的内容安装在带有空格的目录上,它将是甚至更多错误的来源或其名称中的引号(或文字反斜杠)。
{
read _ # consume header
while IFS= read -r dirname; do # ...iterate over later lines...
find "$dirname" -xdev -type d \
'(' -perm -0002 -a ! -perm -1000 ')' \
-exec chmod a+t '{}' + 2>/dev/null
done
} < <(df --output=target --local) # tell df to emit only the one column you want
澄清为什么 xargs是此代码中的错误来源:
-0
[因此需要NUL分隔的输入]或者,在GNU版本中,-d $'\n'
[与换行符分隔符一起正常工作])。这意味着如果您在/media/Movie Name With Spaces
处安装了内容,则xargs会尝试针对find
,/media/Movie
,Name
和With
运行Spaces
分别; /media/Movie Name With Spaces "And Quotes"
也会这样做,但And Quotes
(缺少空格!)只是一个单词。此外,如果你的xargs
代码使用的是现代(2006年代)POSIX扩展,那么在这里使用find
没有效率优势:-exec somecommand {} +
将somecommand
的参数传递给{{1}正如xargs somecommand
所做的那样,每个调用都适合。
答案 1 :(得分:1)
如果您不向chmod
提供参数,则会发生这种情况:
$ chmod a+x
chmod: missing operand after ‘a+x’
Try 'chmod --help' for more information.
所以大概是命令:
df --local -P | awk {'if (NR!=1) print $6'} | \
xargs -I '{}' find '{}' -xdev -type d -perm -0002 -a ! -perm -1000 2>/dev/null
不生成任何输出,即您没有任何目录与要搜索的给定目录中的所需perm
约束匹配。仅运行此命令而不使用chmod
,以确保这一点。