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

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

问题描述

我试图在applescript中处理这个shell脚本,但它一直说错误EOF,因为文件夹名称中的单引号。  do shell scriptcp -R& a& & '& d& / My'Folder / java /& 

文件夹/ My'Folder /是一个合法的目录。



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



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



但是,shell正在卡住处理它,我猜猜,因为文件夹包含在引号中。



有没有什么方法来逃​​避这个单引号,所以它工程在applescript使用shell?



N

div class =h2_lin>解决方案

当使用任意数据作为命令的参数时,始终使用引用的形式。这将总是报价的价值,即使它不需要被引用,但它更安全比对不起。当您有一个单引号的字符串时,您只能使用另一个报价单引号(再次替换模式)。与AppleScript字符串不同,不能转义单引号字符串中的字符。所以你需要打开替代模式,转义一个报价,然后转换替代模式。例如乔的汽车应该被引用为'乔'\\'\\''的汽车'。它是一个带引号的字符串Joe+转义的引号字符+引号字符串s car。但是像我开始你应该使用引用的形式引用的形式Joe的车将返回'Joe'\\\''car'



使用引号格式的命令如下:

 做shell脚本cp -R&引用形式的&空间&引用的形式(d&/ My'Folder / java /)


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/" & "'"

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

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

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

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

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

Cheers

N

解决方案

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天全站免登陆