【问题标题】:Weird Behaviour with New Lines Using grunt-exec使用 grunt-exec 的新行的奇怪行为
【发布时间】:2015-06-14 01:32:32
【问题描述】:

我正在使用 ImageMagick 和 grunt-exec 为我正在使用ImageMagick documentation 中的命令开发的网站生成网站图标。

convert image.png  -bordercolor white -border 0 \
      \( -clone 0 -resize 16x16 \) \
      \( -clone 0 -resize 32x32 \) \
      \( -clone 0 -resize 48x48 \) \
      \( -clone 0 -resize 64x64 \) \
      -delete 0 -alpha off -colors 256 favicon.ico

但是,我在换行方面遇到了一些问题,我认为部分原因是我不完全了解应该如何处理换行。

在我的Gruntfile.js 中,我目前没有使用换行符,它按预期工作。请注意,我必须删除所有换行符并对括号进行双重转义,因为显然 grunt-exec 在执行字符串之前会对其进行解析。

exec: {
    favicon: 'convert _favicon.svg -bordercolor white -border 0 \\( -clone 0 -resize 16x16 \\) \\( -clone 0 -resize 32x32 \\) \\( -clone 0 -resize 48x48 \\) \\( -clone 0 -resize 64x64 \\) -delete 0 -alpha off -colors 256 favicon.ico'
}

就像我说的,我现在可以使用它,但是我希望能够使用换行符来提高可读性,而且我想完全了解这里发生了什么。到目前为止,我已经尝试添加一个额外的 \ 并将所有内容保持不变,将新行替换为 \n 或 \\n 并将所有内容放在同一行但没有运气。

【问题讨论】:

  • 似乎您需要为括号保留 dbl-escapes。此外,请确保每行的结尾 '\' 字符是最后一个字符,没有尾随空格或制表符。 cat -vet file 将显示 $ 作为行尾。如果您在这些行中没有看到“\$”,但看到了“\^I $”或类似内容,则您的编辑器可能会尝试提供帮助并在行中添加空格以使文件中的导航更容易(或其他原因)。祝你好运。

标签: macos bash terminal gruntjs imagemagick


【解决方案1】:

我一直在寻找的解决方案是使用 \n\ 换行,如下所示:

convert image.png  -bordercolor white -border 0 \n\
    \( -clone 0 -resize 16x16 \) \n\
    \( -clone 0 -resize 32x32 \) \n\
    \( -clone 0 -resize 48x48 \) \n\
    \( -clone 0 -resize 64x64 \) \n\
    -delete 0 -alpha off -colors 256 favicon.ico

【讨论】:

    【解决方案2】:

    试试这个:

    exec: {
        favicon: 'convert _favicon.svg -bordercolor white -border 0 "( -clone 0 -resize 16x16 )" "( -clone 0 -resize 32x32 )" "( -clone 0 -resize 48x48 )" "( -clone 0 -resize 64x64 )" -delete 0 -alpha off -colors 256 favicon.ico'
    }
    

    【讨论】:

      猜你喜欢
      • 2020-06-24
      • 1970-01-01
      • 2011-07-31
      • 1970-01-01
      • 2018-02-13
      • 1970-01-01
      • 1970-01-01
      • 2016-11-15
      • 2019-12-04
      相关资源
      最近更新 更多