bash - macOS上的curl与unicode字符的问题

标签 bash macos curl unicode

我有在 MacOS 上运行的脚本 它使用curl 来获取包含共享点站点上的文件的json。

一切正常,但在 bash 或 sh 中运行时,curl 的响应具有以\uxxxx 格式写出的 unicode 字符。

for example ö is \u00f6
"Name":"\u00f6vning.dotx"

但是当使用 zsh 运行时,它可以正确编码。

知道为什么吗?你能使用 bash 或 sh 让它工作吗?

#!/bin/sh
url="https://company.sharepoint.com/sites/Testfiles"
folder="TestDocuments"

files=$(curl -s $url"/_api/web/GetFolderByServerRelativeUrl('Documents/"$folder"')/Files" -H "Accept: application/json")
echo $files

运行curl -v时

GET /sites/Testfiles/_api/web/GetFolderByServerRelativeUrl('Documents/TestDocuments')/Files HTTP/1.1
> Host: company.sharepoint.com
> User-Agent: curl/7.64.1
> Accept: application/json
> 
< HTTP/1.1 200 OK
< Cache-Control: private, max-age=0
< Transfer-Encoding: chunked
< Content-Type: application/json;odata=minimalmetadata;streaming=true;charset=utf-8

这是完整的 json 响应

{
    "odata.metadata": "https://company.sharepoint.com/sites/Testfiles/_api/$metadata#SP.ApiData.Files12",
    "value": [
        {
            "odata.type": "SP.File",
            "odata.id": "https://company.sharepoint.com/sites/Testfiles/_api/Web/GetFileByServerRelativePath(decodedurl='/sites/Testfiles/Documents/TestDocs/\u00f6vning.dotx')",
            "odata.editLink": "Web/GetFileByServerRelativePath(decodedurl='/sites/Testfiles/Documents/TestDocs/%C3%B6vning.dotx')",
            "CheckInComment": "",
            "CheckOutType": 2,
            "ContentTag": "{39C1CD78-3674-49F4-9982-214B33FC03BE},2,5",
            "CustomizedPageStatus": 0,
            "ETag": "\"{39C1CD78-3674-49F4-9982-214B33FC03BE},2\"",
            "Exists": true,
            "IrmEnabled": false,
            "Length": "48725",
            "Level": 1,
            "LinkingUri": "https://company.sharepoint.com/sites/Testfiles/Documents/TestDocs/\u00f6vning.dotx?d=w11c1cd78361119f49982214b33fd43be",
            "LinkingUrl": "https://company.sharepoint.com/sites/Testfiles/Documents/TestDocs/\u00f6vning.dotx?d=w11c1cd78361119f49982214b33fd43be",
            "MajorVersion": 1,
            "MinorVersion": 0,
            "Name": "\u00f6vning.dotx",
            "ServerRelativeUrl": "/sites/Testfiles/Documents/TestDocs/\u00f6vning.dotx",
            "TimeCreated": "2021-10-14T13:32:16Z",
            "TimeLastModified": "2021-10-14T13:32:16Z",
            "Title": "",
            "UIVersion": 512,
            "UIVersionLabel": "1.0",
            "UniqueId": "39c1cd78-3674-49f4-9982-214b33fc03be"
        }
    ]
}

最佳答案

bash ,您需要使用-e选项有echo展开\u转义为终端可以显示的 UTF-8 序列。

$ x='"\u00f6vning.dotx"'
$ echo "$x"
"\u00f6vning.dotx"
$ echo -e "$x"
"övning.dotx"

首先,我们有一个“巧合”,即 JSON,bashzsh使用相同的语法 ( \u.... ) 以纯 ASCII 表示任意 Unicode 代码点。对于一般的 POSIX 兼容 shell 来说,这不是,所以我认为您不能期望它在运行 sh 时起作用。 ,无论实际使用哪个 shell。 (实际上,它不适用于 bash 3.2,但适用于 bash 的更高版本。)

zsh echo 的实现与 XSI 兼容,因为它默认扩展各种字符序列。 ( \u 本身不是由 XSI 定义的,但 zsh 将它们包含在由 echo 扩展的序列列表中。)( echo 的 POSIX 合规性相当宽松,仅说明包含反斜杠的参数可以在特定于实现的方式。)

bash echo的实现默认情况下符合XSI。 -e启用反斜杠序列的扩展,就像设置xpg_echo一样shell 选项。

$ shopt -s xpg_echo
$ echo "$x"
"övning.dotx"

关于bash - macOS上的curl与unicode字符的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69572460/

相关文章:

linux - Bash 文本解析高尔夫

linux - 如何在Linux中对以数字命名的子目录进行排序

regex - 在 SED 中转义正则表达式

macos - 是否有 API(或任何 hack)可以访问 Mac OS X Mavericks 中的增强听写功能?

linux - 将 curl 重定向到 while 循环

bash - 无法通过读取比较接收到的用户输入

macos - 使用TextMate打开Grails项目

ssl - curl --cacert 错误 "curl: (60) SSL: no alternative certificate subject name matches target host name"

c++ - 无法获得协商身份验证以在 libcurl 程序中工作

c - 将文件写入 "DiagnosticReports"时访问被拒绝