linux - chmod : missing operand after a+t

标签 linux bash shell

尝试修复全局可写文件并出现以下错误:

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 运行 findName, 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/

相关文章:

用于检查互联网连接不工作的 Linux bash 脚本

git - .tmux.conf : update status commands on panel focus

xml - POST XML 数据返回 HTTP/1.0 501 Not Implemented 错误

linux - Make 正在运行旧的/损坏的命令

linux - bin/bash 嵌套循环不起作用

bash - 什么是删除一行的前 N ​​个字符的 unix 命令?

linux - 搜索早于特定时间的文件并将其移动到特定文件夹中

linux - 使用 zsh 时如何自动启 Action 业

c++ - 启动停止守护进程不发送 SIGTERM

c - 在linux中将.a文件与.so共享库文件绑定(bind)