如何在 AppleScript 中转义 shell 参数?
Applescript 似乎无法正确转义字符串。我做错了什么?
示例:
set abc to "funky-!@#'#\"chars"
display dialog abc
display dialog quoted form of abc
预期/所需输出:
funky-!@#'#"chars
'funky-!@#\'#"chars'
实际输出:
funky-!@#'#"chars
'funky-!@#'\''#"chars'
如您所见,在实际输出中 Applescript 似乎添加并转义了额外的 '
我可以接受结束字符为 '
或 "
并且我也可以同时转义单引号和双引号 - 但似乎只有单引号实际上被转义。
Applescript does not seem to properly escape strings. What am I doing wrong?
Example:
set abc to "funky-!@#'#\"chars"
display dialog abc
display dialog quoted form of abc
Expected / Desired Output:
funky-!@#'#"chars
'funky-!@#\'#"chars'
Actual Output:
funky-!@#'#"chars
'funky-!@#'\''#"chars'
As you can see, it appears that in the actual output Applescript is adding and escaping an extra '
I would be OK with the end characters being either '
or "
and I would also be fine with both the single and double quotes being escaped - but it appears that only the single quotes are actually escaped.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
shell 中的单引号内的反斜杠通常不被解释。
然而,它们由 sh 中的 echo 进行解释,sh 是
do shell script
使用的 shell:取消设置
xpg_echo
使其行为类似于 bash 中的 echo:通常使用 HEREDOC 更简单改为重定向:
Backslashes aren't usually interpreted inside single quotes in shells.
However they are interpreted by echo in sh, which is the shell used by
do shell script
:Unsetting
xpg_echo
makes it behave like the echo in bash:Often it's simpler to use HEREDOC redirection instead:
使用“引用形式”。一般来说,在 applescript 中,我们正在处理“mac”样式路径,因此我们会执行类似的操作将其传递给 shell...
Use "quoted form of". In general in applescript we are dealing with a "mac" style path so we would do something like this to pass it to the shell...
不,
'funky-!@#'''#"chars'
中没有添加额外的'
。正如
17510427541297
已经指出的, 的引用形式
惯用语适用于 Unix shell,如果 Unix shell 中的字符串直接彼此相邻放置,则会将它们连接起来。AppleScript
带引号的 abc 形式
只是这样做:它创建一个用单引号括起来的字符串,但将该字符串中的每个单引号'
替换为'''
这会创建三个单独的字符串,但是这三个单独的字符串在(大多数)Unix shell 中受以下字符串连接机制的约束:
"funky-!@#'#"chars"
变成'funky-!@#'
+'
+'#"chars'
生成的字符串适合由 Unix shell 解释为单个字符串文字字符串(不会导致参数扩展问题等)。
No, there is no extra
'
added in'funky-!@#'''#"chars'
.As already indicated by
17510427541297
, AppleScript'squoted form of
idiom is meant for use in Unix shells, and strings in Unix shells get concatenated if they are placed directly next to each other.AppleScript's
quoted form of abc
just does this: it creates a string enclosed by single quotes, but replaces every single quote'
withing that string with'''
.This, in fact, creates three separate strings, but the three separate strings are subject to the following string concetanation mechanism in (most) Unix shells:
"funky-!@#'#"chars"
becomes'funky-!@#'
+'
+'#"chars'
The resulting string is fit to get interpreted by Unix shells as a single literal string (without causing parameter expansion issues and the like).