bash chmod:a+t 后缺少操作数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39906764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
chmod: missing operand after a+t
提问by Blooze
Trying to fix world writable files and getting the error below:
尝试修复世界可写文件并收到以下错误:
chmod: missing operand after a+t
here is my code
这是我的代码
df --local -P | awk {'if (NR!=1) print '} | xargs -I '{}' find '{}' -xdev -type d -perm -0002 -a ! -perm -1000 2>/dev/null | xargs chmod a+t
Any thoughts?
有什么想法吗?
回答by Charles Duffy
There's no need for xargs
here at all -- it's the source of your bugs, and would be the source of even morebugs if you had contents mounted on a directory with spaces or quotes (or literal backslashes) in its name.
这里根本不需要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
To clarify whyxargs is a source of bugs in this code:
澄清为什么xargs 是此代码中的错误来源:
- GNU xargs (but not BSD xargs) will call the command with no arguments whatsoever if its input has no contents. This is your immediate problem at hand.
- All POSIX-compliant xargs implementations will parse quotes and backslashes (consuming them), and split input on whitespace unless extensions are used to direct it to do otherwise (such as
-0
[thereby requiring NUL-delimited inputs], or, in the GNU version,-d $'\n'
[to work correctly with newline delimiters]). This means if you had content mounted at/media/Movie Name With Spaces
, xargs would be trying to runfind
against/media/Movie
,Name
,With
, andSpaces
separately;/media/Movie Name With Spaces "And Quotes"
would do likewise, but withAnd Quotes
(lacking the spaces!) as a single word.
- 如果其输入没有内容,GNU xargs(但不是 BSD xargs)将调用不带任何参数的命令。这是您眼前的问题。
- 所有符合 POSIX 的 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
(缺少空格!)作为一个词。
Moreover, there's no efficiency benefit to using xargs
here, if your find
code is using modern (2006-era) POSIX extensions: -exec somecommand {} +
passes as many arguments to somecommand
as will fit on each invocation, just as xargs somecommand
does.
此外,xargs
如果您的find
代码使用现代(2006 年时代)POSIX 扩展,则在此处使用没有效率优势:-exec somecommand {} +
将尽可能多的参数传递给somecommand
适合每次调用的参数,就像xargs somecommand
这样。
回答by heemayl
This happens when you provide no argument to chmod
:
当您不提供任何参数时会发生这种情况chmod
:
$ chmod a+x
chmod: missing operand after ‘a+x'
Try 'chmod --help' for more information.
So presumably, the command:
所以大概,命令:
df --local -P | awk {'if (NR!=1) print '} | \
xargs -I '{}' find '{}' -xdev -type d -perm -0002 -a ! -perm -1000 2>/dev/null
is not generating any output i.e. you don't have any directory with matching the required perm
constraints in the given directories to be searched. Run only this command, without chmod
, to be sure of this.
不生成任何输出,即您没有任何目录与perm
要搜索的给定目录中的所需约束相匹配。仅运行此命令,不运行chmod
,以确保这一点。