如何在 AppleScript 中转义 shell 参数?

发布于 2024-12-15 19:17:34 字数 471 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谁的新欢旧爱 2024-12-22 19:17:35

shell 中的单引号内的反斜杠通常不被解释。

将字符括在单引号中可保留单引号内每个字符的字面值。单引号内不能出现单引号。

反斜杠不能用于转义用单引号设置的字符串中的单引号。可以通过编写来创建嵌入的引号,例如:'a'\''b',生成 a'b。

然而,它们由 sh 中的 echo 进行解释,sh 是 do shell script 使用的 shell:

do shell script "echo " & quoted form of "\\t" --> "\t"

取消设置 xpg_echo 使其行为类似于 bash 中的 echo:

do shell script "shopt -u xpg_echo; echo " & quoted form of "\\t" --> "\\t"

通常使用 HEREDOC 更简单改为重定向:

do shell script "rev <<< " & quoted form of "a\\tb" --> "b\\ta"

Backslashes aren't usually interpreted inside single quotes in shells.

Enclosing characters in single quotation marks preserves the literal value of each character within the single quotation marks. A single quotation mark cannot occur within single quotation marks.

A backslash cannot be used to escape a single quotation mark in a string that is set in single quotation marks. An embedded quotation mark can be created by writing, for example: 'a'\''b', which yields a'b.

However they are interpreted by echo in sh, which is the shell used by do shell script:

do shell script "echo " & quoted form of "\\t" --> "\t"

Unsetting xpg_echo makes it behave like the echo in bash:

do shell script "shopt -u xpg_echo; echo " & quoted form of "\\t" --> "\\t"

Often it's simpler to use HEREDOC redirection instead:

do shell script "rev <<< " & quoted form of "a\\tb" --> "b\\ta"
梦明 2024-12-22 19:17:35

使用“引用形式”。一般来说,在 applescript 中,我们正在处理“mac”样式路径,因此我们会执行类似的操作将其传递给 shell...

set theFile to choose file
set dirname to do shell script "dirname " & quoted form of POSIX path of theFile

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...

set theFile to choose file
set dirname to do shell script "dirname " & quoted form of POSIX path of theFile
亣腦蒛氧 2024-12-22 19:17:35

不,'funky-!@#'''#"chars' 中没有添加额外的 '

正如 17510427541297 已经指出的, 的引用形式 惯用语适用于 Unix shell,如果 Unix shell 中的字符串直接彼此相邻放置,则会将它们连接起来。

AppleScript 带引号的 abc 形式 只是这样做:它创建一个用单引号括起来的字符串,但将该字符串中的每个单引号 ' 替换为 '''

这会创建三个单独的字符串,但是这三个单独的字符串在(大多数)Unix shell 中受以下字符串连接机制的约束:

"funky-!@#'#"chars"变成'funky-!@#' + ' + '#"chars'

生成的字符串适合由 Unix shell 解释为单个字符串文字字符串(不会导致参数扩展问题等)。

# in Terminal.app
# note the escaping in: osascript -e '...'\''...'
quotedsrt="$(osascript -e '
set abc to "funky-!@#'\''#\"chars"
return quoted form of abc
')"

echo "$quotedsrt"                 # 'funky-!@#'\''#"chars'
eval echo "$quotedsrt"            # funky-!@#'#"chars
echo echo "$quotedsrt" | sh


# escaping mechanism for Bash shell
set +H
esc="'\''"
str="funky-!@#'#\"chars"
str="'${str//\'/${esc}}'"
set -H

echo "$str"                  # 'funky-!@#'\''#"chars'
eval echo "$str"             # funky-!@#'#"chars
echo echo "$str" | sh

No, there is no extra ' added in 'funky-!@#'''#"chars'.

As already indicated by 17510427541297, AppleScript's quoted 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).

# in Terminal.app
# note the escaping in: osascript -e '...'\''...'
quotedsrt="$(osascript -e '
set abc to "funky-!@#'\''#\"chars"
return quoted form of abc
')"

echo "$quotedsrt"                 # 'funky-!@#'\''#"chars'
eval echo "$quotedsrt"            # funky-!@#'#"chars
echo echo "$quotedsrt" | sh


# escaping mechanism for Bash shell
set +H
esc="'\''"
str="funky-!@#'#\"chars"
str="'${str//\'/${esc}}'"
set -H

echo "$str"                  # 'funky-!@#'\''#"chars'
eval echo "$str"             # funky-!@#'#"chars
echo echo "$str" | sh
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文