【问题标题】:Strip off characters from xargs {} in bash在 bash 中从 xargs {} 中去除字符
【发布时间】:2016-05-19 14:19:25
【问题描述】:

假设我有名为 "*.data.done" 的文件。现在我想将它们(递归地)重命名为“*.data”,然后是包含"pattern"

所以我们开始:

grep -l -R -F "pattern" --include '*.data.done' * | xargs -I{} mv {} ${{}::-5}

好吧,这种“.done”的剥离不起作用(bash 4.3.11):

bash: ${{}::-5}: bad substitution

我怎样才能做到这一点最简单的方法?

【问题讨论】:

  • 我会在文件moveit.sh 中创建一个小脚本:for file in "$@"; do mv "$file" "${file%.done}"; done,然后使用xargs sh moveit.sh。这很简单,清理moveit.sh 文件的开销并不大。
  • {} 不是shell变量,不能对其进行shell参数扩展。

标签: bash substitution xargs string-substitution


【解决方案1】:

占位符{} 不能用于${...} 内的 BASH 字符串操作。

你可以使用:

grep -lRF "pattern" --include '*.data.done' . |
xargs -I{} bash -c 'f="{}"; mv "$f" "${f/.done}"'

但是,如果您想避免为每个文件生成子 shell,请使用 for 循环:

while IFS= read -d '' -r f; do
    mv "$f" "${f/.done}"
done < <(grep -lRF "pattern" --include '*.data.done' --null .)

【讨论】:

    猜你喜欢
    • 2017-04-06
    • 2010-11-30
    • 1970-01-01
    • 1970-01-01
    • 2012-09-13
    • 2017-05-24
    • 2013-09-23
    • 1970-01-01
    相关资源
    最近更新 更多