在shell脚本中转​​义引号引起麻烦 [英] Trouble escaping quotes in a shell script

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

问题描述

我正在制作一个bash脚本,该脚本会生成一些数字,将其分配给变量,然后使用osascript在任意选择的应用程序中将其写出.

I have a bash script I am making that generates some numbers assignes them to variables then uses osascript to write them out in any application of choice.

这是我想做的事情的简化版本.

Here is a simplified version of what I want to do.

monday1=5
osascript -e 'tell application "System Events" to keystroke "$monday1"'; \

该osascript应该看起来像这样

The osascript should look like this

osascript -e 'tell application "System Events" to keystroke "5"'; \

这将在我光标所在的任何应用程序中键入数字5.问题是它改为输出$ monday1.我知道我必须做一些事来转义引号,以便脚本可以输入变量,但是我不确定如何.

This will then type out the number 5 in what ever app I have my cursor in. The problem is it outputs $monday1 instead. I know I have to do something to escape the quotes so the script can input the variable, but I'm not sure how.

有想法吗?

推荐答案

问题在于单引号内没有特殊的元字符,因此$monday1会原样传递给osascript.因此,必须确保$monday1不在单引号内.

The problem is that inside single quotes, there are no special metacharacters, so $monday1 is passed to osascript unchanged. So, you have to make sure that $monday1 is not inside single quotes.

您的选择包括:

monday1=5
osascript -e 'tell application "System Events" to keystroke "'$monday1'"'

monday1=5
osascript -e 'tell application "System Events" to keystroke "'"$monday1"'"'

monday1=5
osascript -e "tell application \"System Events\" to keystroke \"$monday1\""

monday1=5
osascript -e "tell application 'System Events' to keystroke '$monday1'"

第一个在双引号之后停止单引号以包围键击,嵌入$monday的值,并为剩余的双引号恢复单引号.之所以有效,是因为$monday1中没有空格.

The first stops the single-quoting just after the double quote to surround the key stroke, embeds the value of $monday, and resumes the single-quoting for the remaining double quote. This works because there are no spaces etc in $monday1.

第二个类似于第一个,但是用双引号括住"$monday1",因此即使$monday1包含空格也可以使用.

The second is similar to the first but surrounds "$monday1" in double quotes, so it will work even if $monday1 contained spaces.

第三个参数将整个参数用双引号引起来,并转义每个嵌入的双引号.如果您对反斜杠不过敏,那就很好.

The third surrounds the whole argument with double quotes, and escapes each embedded double quote. If you're not allergic to backslashes, this is good.

第四种方法可能有效或无效,这取决于osascript程序是否对围绕其参数的引号类型敏感.它只是颠倒了双引号和单引号的使用. (就外壳而言,这是可行的.戈登·戴维森

The fourth may or may not work — it depends on whether the osascript program is sensitive to the type of the quotes surrounding its arguments. It simply reverses the use of double quotes and single quotes. (This works as far as the shell is concerned. Gordon Davisson observes in a comment that osascript requires double quotes and does not accept single quotes, so it doesn't work because of the rules of the invoked program — osascript.)

在第三种情况和第四种情况下,都需要注意字符串的其他部分是否需要保护,以使外壳程序不会扩展信息.通常,单引号会更好,因此我将使用前两个选项之一.

In both the third and fourth cases, you need to be careful if there are other parts of the string that need to be protected so that the shell does not expand the information. In general, single quotes are better, so I'd use one of the first two options.

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

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