osascript使用bash变量用空格 [英] osascript using bash variable with a space

查看:470
本文介绍了osascript使用bash变量用空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 osascript 中的Bash显示在通过苹果脚本通知中心(Mac OS X中)的消息。我想从猛砸传递文本变量到脚本。对于没有空格的变量,这个工作得很好,但不是一个用空格:

I am using osascript in Bash to display a message in Notification Center (Mac OS X) via Apple Script. I am trying to pass a text variable from Bash to the script. For a variable without spaces, this works just fine, but not for one with spaces:

定义

var1="Hello"
var2="Hello World"

和使用

osascript -e 'display notification "'$var1'"'

的作品,但使用

osascript -e 'display notification "'$var2'"'

收益

syntax error: Expected string but found end of script.

什么我需要改变(我是新来这个)?谢谢!

What do I need to change (I am new to this)? Thanks!

推荐答案

您可以尝试改用:

 osascript -e "display notification \"$var2\""

或者

osascript -e 'display notification "'"$var2"'"'

该修复的变量操纵包含在bash空间的问题。然而,这种解决方案确实对osascript code注射没有什么。因此,这将是更好的选择查尔斯·达菲的解决方案之一或使用庆典参数扩展:

This fixes the problem of manipulation of variables that contains spaces in bash. However, this solution doesn't anything against injections of osascript code. So it would be better to choose one of Charles Duffy's solutions or to use bash parameter expansion :

# if you prefer escape the doubles quotes
osascript -e "display notification \"${var2//\"/\\\"}\""
# or
osascript -e 'display notification "'"${var2//\"/\\\"}"'"'

# if you prefer to remove the doubles quotes
osascript -e "display notification \"${var2//\"/}\""
# or
osascript -e 'display notification "'"${var2//\"/}"'"'

感谢到mklement0对这个非常有用的建议!

Thank to mklement0 for this very useful suggestion !

这篇关于osascript使用bash变量用空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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