macos 如何在 AppleScript 中转义 shell 参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8138167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 08:36:48  来源:igfitidea点击:

How can I escape shell arguments in AppleScript?

macosescapingapplescriptarguments

提问by cwd

Applescript does not seem to properly escape strings. What am I doing wrong?

Applescript 似乎没有正确转义字符串。我究竟做错了什么?

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 '

如您所见,在实际输出中,Applescript 似乎添加并转义了一个额外的 '

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.

我可以将结束字符设为'or"并且我也可以将单引号和双引号都转义 - 但似乎只有单引号实际上被转义了。

回答by Lri

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

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

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.

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

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

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

但是,它们由 sh 中的 echo 解释,这是 sh 使用的外壳do shell script

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

Unsetting xpg_echomakes it behave like the echo in bash:

取消设置xpg_echo使其表现得像 bash 中的 echo:

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

Often it's simpler to use HEREDOC redirection instead:

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

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

回答by regulus6633

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

使用“引用形式”。一般来说,在applescript中,我们正在处理一个“mac”样式的路径,所以我们会做这样的事情来将它传递给shell......

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

回答by chad

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

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

As already indicated by 17510427541297, AppleScript's quoted form ofidiom is meant for use in Unix shells, and strings in Unix shells get concatenated if they are placed directly next to each other.

正如 已经指出的那样17510427541297,AppleScript 的quoted form of习语旨在用于 Unix shell,如果 Unix shell 中的字符串直接彼此相邻放置,则它们会被连接起来。

AppleScript's quoted form of abcjust does this: it creates a string enclosed by single quotes, but replaces every single quote 'withing that string with '\''.

AppleScriptquoted form of abc就是这样做的:它创建一个由单引号括起来的字符串,但''\''.

This, in fact, creates three separate strings, but the three separate strings are subject to the following string concetanation mechanism in (most) Unix shells:

这实际上创建了三个单独的字符串,但是这三个单独的字符串在(大多数)Unix shell 中受以下字符串混淆机制的约束:

"funky-!@#'#\"chars"becomes 'funky-!@#'+ \'+ '#"chars'

"funky-!@#'#\"chars"变成'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).

结果字符串适合被 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