我如何防止ZSH替换“%5D” “]&quot?
[编辑]解决。 实际问题是一个处理程序变量之一中的错字,而不是我最初认为的ZSH问题。
我试图防止ZSH更换代码“ %%5D
”带有实际字符“ ]
”的右方平方括号。
出于我的目的 - 通过shell脚本中的卷曲来调用last.fm api,以获取有关苹果音乐的曲目的信息 - 它需要保持为“ %5D
>”,因为否则API不会认出它。 我尝试用后斜切或第二个“%”逃脱“%5D”,但这仍然不起作用,它仍然被“]”
取代,这是我要做的事情的示例,什么不是工作:
曲目名称的原始字符串: “ Yestoday(扩展版本)[BONUS TRACK]
“
如何拼写API工作: “ Yestoday+(扩展+版本)+%5BBONUS+TRACK%5D
“
Whitevess被我的AppleScript处理程序替换为“+”。 左[
停留为“%5B”(我想是因为它紧接着是更多的字母,因此ZSH无法将其识别为代码并替换)。 正确的]
通常是轨道字符串的最后一个字符,因此没有任何内容,因此“%5D”被ZSH识别为“]”。
我该如何解决? 非常感谢任何帮助:)
参考: AppleScript的一部分应该用代码替换“ []”的一部分。 (这显然不是完整的脚本)。如果运行此操作,它将正确替换[]。仅在用进行shell脚本curl_command
进行整个API调用后才出现问题,因为ZSH解释了代码。
set trackreplace to "YESTODAY (Extended Version) [Bonus Track]"
if trackreplace contains "[" then
set trackreplace to replaceChars("[", "%5B", trackreplace)
end if
if trackreplace contains "]" then
set trackeplace to replaceChars("]", "%5D", trackreplace)
end if
on replaceChars(find, replace, subject)
set savedelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to find
set subject to text items of subject
set AppleScript's text item delimiters to replace
set subject to (subject as string)
set AppleScript's text item delimiters to savedelims
return subject
end replaceChars
[EDIT] Solved.
The actual issue was a typo in one of the handler variables, and not a zsh issue, as I initially thought.
I am trying to prevent zsh from replacing the code "%5D
" for the ] right square bracket with the actual character "]
".
For my purposes - calling the last.fm API via curl in shell script, to get info about a track from Apple Music - it needs to stay as "%5D
", because otherwise the API doesn't recognise it.
I have tried escaping the "%5D" with backslashes or a second "%" but that doesn't work, it still gets replaced with "]"
Here's an example of what I'm trying to do and what is not working:
The original string of the track's name:
"YESTODAY (Extended Version) [Bonus Track]
"
How it needs to be spelled for the API to work:
"YESTODAY+(Extended+Version)+%5BBonus+Track%5D
"
The whitespaces get replaced with "+" by my AppleScript handler.
The left [
stays as "%5B" (I guess because it is immediately followed by more letters and therefore zsh cannot recognise it as code and replace it).
The right ]
is generally the last character of the track string, not followed by anything and thus "%5D" is recognised by zsh and written as "]" .
How do I fix this?
Any help is greatly appreciated :)
For reference:
The part of the AppleScript that is supposed to replace the "[ ]" with code.
(This is obviously not the complete script). If you run this, it replaces the [ ] correctly. The issue only arises once the entire API call is made with do shell script curl_command
because of zsh interpreting the code.
set trackreplace to "YESTODAY (Extended Version) [Bonus Track]"
if trackreplace contains "[" then
set trackreplace to replaceChars("[", "%5B", trackreplace)
end if
if trackreplace contains "]" then
set trackeplace to replaceChars("]", "%5D", trackreplace)
end if
on replaceChars(find, replace, subject)
set savedelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to find
set subject to text items of subject
set AppleScript's text item delimiters to replace
set subject to (subject as string)
set AppleScript's text item delimiters to savedelims
return subject
end replaceChars
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它不是 ,但实际上只是一个处理程序变量之一(
trackeplace
而不是trackreplace
),如 @指出的那样。 GordondavissonIt was not a zsh issue but actually just a typo in one of the handler variables (
trackeplace
instead oftrackreplace
), as pointed out by @GordonDavisson