【问题标题】:How to use base64 command with CURL?如何在 CURL 中使用 base64 命令?
【发布时间】:2017-08-07 18:50:28
【问题描述】:

如果我尝试对字体文件进行 base64 编码并将内容输出到文本文件,如下所示:

base64 myfont.woff2 > output.txt

考虑到字体存储在本地,效果很好。但是,如果我尝试从 web url 和 base64 卷曲字体输出它会导致一堆乱码被保存到文件中,例如:

#!/bin/bash
declare -a arr=(
    "https://fonts.gstatic.com/s/opensans/v14/K88pR3goAWT7BTt32Z01mxJtnKITppOI_IvcXXDNrsc.woff2"
)

for i in "${arr[@]}"
do
    content="$(curl -s "$i")"
    echo base64 "$content" > output.txt
done

如何让它工作?

【问题讨论】:

    标签: bash shell curl


    【解决方案1】:

    您可以通过管道将curl 输出到base64

    for i in "${arr[@]}"; do
       curl -s "$i" | base64
    done > output.txt
    

    【讨论】:

      【解决方案2】:

      base64 命令期望将文本解码为输入,而不是作为参数。你可以直接用管道 curl 给它:

      curl -s "$url" | base64
      

      或者,一旦您在原始代码中将 curl 结果捕获为变量,请使用此处的字符串:

      base64 <<<"$content"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-15
        • 2014-07-26
        • 2014-05-18
        • 2014-08-26
        相关资源
        最近更新 更多