How can I escape shell arguments in AppleScript?

大兔子大兔子 提交于 2019-11-29 15:32:25

问题


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.


回答1:


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"



回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/8138167/how-can-i-escape-shell-arguments-in-applescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!