尝试修复全局可写文件并出现以下错误:
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
有什么想法吗?
最佳答案
这里根本不需要 xargs
-- 它是错误的来源,如果您将内容安装在名称中包含空格或引号(或文字反斜杠)的目录。
{
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 是此代码中错误的来源:
- 如果输入没有内容,GNU xargs(但不是 BSD xargs)将调用不带任何参数的命令。这是您当前面临的问题。
- 所有符合 POSIX 标准的 xargs 实现都将解析引号和反斜杠(使用它们),并在空格处拆分输入,除非使用扩展来指示它以其他方式执行(例如
-0
[因此需要 NUL -delimited inputs],或者,在 GNU 版本中,-d $'\n'
[正确使用换行符])。这意味着如果您将内容安装在/media/Movie Name With Spaces
,xargs 将尝试针对/media/Movie
运行find
,Name
,With
,Spaces
分开;/media/Movie Name With Spaces "And Quotes"
也会这样做,但And Quotes
(缺少空格!)作为一个词。
此外,如果您的 find
代码使用的是现代(2006 年时代)POSIX 扩展,那么在此处使用 xargs
没有任何效率优势:-exec somecommand { } +
向 somecommand
传递适合每次调用的参数,就像 xargs somecommand
一样。
关于linux - chmod : missing operand after a+t,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39906764/