【问题标题】:bash How to escape specialcharacter '@' in filename template?bash 如何在文件名模板中转义特殊字符'@'?
【发布时间】:2023-03-06 20:36:02
【问题描述】:

如何在搜索文件名模板中转义特殊字符@

此代码不起作用(空结果)

for file in *_@2.png; do echo "$file"; done

也试过这个:

for i in *_@2.png; do mv "$i" "${i/_@2.png}"@2x.png; done

我需要将文件 *_@2.png 重命名为 *@2x.png

【问题讨论】:

  • 使用反斜杠进行转义
  • 它对 *\@2.png 中的文件也不起作用;做回声“$文件”;完成我得到 *\@2.png
  • 或使用单引号
  • \@2.png; do echo "$file"; done I get \@2.png?
  • 仅适用于 *.png 文件;做回声“$文件”;完成

标签: bash templates filenames


【解决方案1】:

检查文件是否存在

for file in *_@2.png; do
    [[ -e $file ]] || continue  # to skip file which not exists
    ...
done

使用 shell 选项。 如果在不匹配 glob 时未设置 failglob 选项,则将 glob *_@2.png 扩展为自身

shopt globfail     # to check value
shopt -s globfail  # to turn on
shopt -u globfail  # to turn off

【讨论】:

    【解决方案2】:

    解决了

    for i in *.png; do mv "$i" "${i/_@2.png/@2x.png}" ; done
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
    猜你喜欢
    • 1970-01-01
    • 2013-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多