我有在 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/övning.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"
}
]
}
uj5u.com熱心網友回復:
在 中bash,您需要使用-e選項echo將\u轉義擴展為終端可以顯示的 UTF-8 序列。
$ x='"\u00f6vning.dotx"'
$ echo "$x"
"\u00f6vning.dotx"
$ echo -e "$x"
"?vning.dotx"
首先,我們有一個“巧合”JSON,bash并zsh使用相同的語法 ( \u....) 以純 ASCII 表示任意 Unicode 代碼點。這是不是在一般符合POSIX的炮彈真實的,所以我不認為你可以期望運行時,這個作業sh,無論哪個殼實際使用的。(實際上,它不適用于bash3.2,但適用于更高版本的bash.)
zsh的實作echo是 XSI 兼容的,因為它默認擴展各種字符序列。(\u它本身不是由 XSI 定義的,但將zsh它們包含在由 擴展的序列串列中echo。)(POSIX 符合性echo相當松散,僅說明可以以特定于實作的方式處理包含反斜杠的引數。)
bash默認情況下,echo的實作不符合 XSI。-e啟用反斜杠序列的擴展,就像設定xpg_echoshell 選項一樣。
$ shopt -s xpg_echo
$ echo "$x"
"?vning.dotx"
uj5u.com熱心網友回復:
要在 bash 和 zsh 上使用相同的命令:
printf "%b\n" "\u00f6vning.dotx"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/315320.html
