单引号在applescript/shell中转义 [英] single quote escaping in applescript/shell

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

问题描述

我正在尝试在 applescript 中处理这个 shell 脚本,但它一直说错误 EOF,因为文件夹名称中有单引号.

I'm trying to process this shell script in applescript, but it keeps saying error EOF, because of the single quote in the folder name.

do shell script "cp -R " & a & " " & "'" & d & "/My 'Folder/java/" & "'"

文件夹/My 'Folder/是合法目录.

The folder /My 'Folder/ is a legitimate directory.

变量 a = '/Applications/MyProgram/'(包括单引号)

The variable a = '/Applications/MyProgram/' (and includes the single quotes)

变量 d =/Folders(没有单引号)

The variable d = /Folders (with no single quotes)

但是,shell 在处理它时遇到困难,我猜是因为文件夹被引号括起来了.

However, shell is getting stuck processing it, im guessing because the folder is enclosed in quotes.

有什么办法可以转义这个单引号,所以它可以在使用 shell 的 applescript 中工作吗?我试过多个反斜杠,但它不起作用.

Is there any way to escape this single quote, so it works in applescript using shell? Ive tried multiple backslashes but its not working.

干杯

N

推荐答案

在使用任意数据作为命令的参数时,始终使用 quoted form of.这将始终引用该值,即使它不需要引用,但它比抱歉更安全.当您有一个字符串单引号时,您只能用另一个引号取消引用(重新打开替换模式).与 AppleScript 字符串不同,您不能对单引号字符串中的字符进行转义.因此,您需要打开替换模式,转义引号,然后将替换模式转回.例如,Joe's car"应该被引用为'Joe'\\''s car'".这是一个带引号的字符串Joe"+转义的引号字符+带引号的字符串s car".但是就像我开始时一样,您应该使用 quoted form ofquoted form of "Joe's car" 将返回 "'Joe'\\''s car'"

Always use quoted form of when using arbitrary data as an argument to an command. This will always quote the value even if it doesn't need to be quoted but it's better safe than sorry. When you have a string single quoted, you can only unquote (turn substitution mode back on) with another quote. Unlike AppleScript strings, you can't escape characters inside single quoted strings. So you need to turn substitution mode on, escape a quote and then turn substitution mode back one. For instance "Joe's car" should be quoted as "'Joe'\\''s car'". It's a quoted string "Joe" + escaped quote character + quoted string "s car". But like I started you should use quoted form of, quoted form of "Joe's car" will return "'Joe'\\''s car'"

您使用引用形式的命令如下所示:

Your command using quoted form will look like:

do shell script "cp -R " & quoted form of a & space & quoted form of (d & "/My 'Folder/java/")

这篇关于单引号在applescript/shell中转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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