如何在 AppleScript 中转义 shell 参数? [英] How can I escape shell arguments in AppleScript?

查看:31
本文介绍了如何在 AppleScript 中转义 shell 参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

示例:

set abc to "funky-!@#'#\"chars"
display dialog abc
display dialog quoted form of abc

预期/期望输出:

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

实际输出:

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

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

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.

推荐答案

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

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'\''b',产生 a'b.

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.

但是它们被sh中的echo解释,这是do shell script使用的shell:

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"

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

Unsetting xpg_echo makes it behave like the echo in bash:

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

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

Often it's simpler to use HEREDOC redirection instead:

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

这篇关于如何在 AppleScript 中转义 shell 参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆