【问题标题】:Escape variables with filenames that have spaces使用带空格的文件名转义变量
【发布时间】:2016-08-21 15:56:43
【问题描述】:

我正在使用这一行来批处理我的pngcrush,但我的文件包含空格,这些空格按字面意思放置在$line 中,由于它们不是有效路径,因此会被跳过:

ls *.png | while read line; do pngcrush -brute $line compressed/$line; done

我怎样才能让 $line 成为转义,就像这个文件名 Button - Users.png 将被替换为 Button\ -\ Users.png

【问题讨论】:

    标签: bash pngcrush


    【解决方案1】:

    Don't parse the output of ls。在此处使用 for 循环。

    for f in *.png; do
        pngcrush -brute "$line" compressed/"$line"
    done
    

    【讨论】:

      【解决方案2】:

      只需用双引号将变量括起来。这是一个很好的做法:

      ls *.png | while read line; do pngcrush -brute "$line" "compressed/$line"; done
      

      【讨论】:

        猜你喜欢
        • 2013-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多