加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

macos – 在Bash中组合变量以形成使用osascript命令发送到AppleS

发布时间:2020-12-15 18:24:43 所属栏目:安全 来源:网络整理
导读:我正在尝试使这个脚本工作.它是一个 Bash脚本,用于获取一些变量,将它们放在一起并使用结果发送 AppleScript命令.手动粘贴从osascript -e后面的变量to_osa回显到终端的字符串按照我的意愿工作并期望它.但是当我尝试将命令osascript -e和字符串to_osa结合起来
我正在尝试使这个脚本工作.它是一个 Bash脚本,用于获取一些变量,将它们放在一起并使用结果发送 AppleScript命令.手动粘贴从osascript -e后面的变量to_osa回显到终端的字符串按照我的意愿工作并期望它.但是当我尝试将命令osascript -e和字符串to_osa结合起来时,它不起作用.我怎样才能做到这一点?
the_url=""https://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash""
the_script='tell application "Safari" to set the URL of the front document to '
delimiter="'"
to_osa=${delimiter}${the_script}${the_url}${delimiter}
echo ${to_osa}
osascript -e ${to_osa}

除了手动工作之外,当我将所需命令写入脚本然后执行它时,脚本也可以工作:

echo "osascript -e" $to_osa > ~/Desktop/outputfile.sh
sh  ~/Desktop/outputfile.sh
作为一般规则,不要在变量中加入双引号,将它们放在变量周围.在这种情况下,它更复杂,因为你有一些用于bash级引用的双引号,还有一些用于AppleScript级引用;在这种情况下,AppleScript级引号放在变量中,bash级引号围绕变量:
the_url=""https://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash""
the_script='tell application "Safari" to set the URL of the front document to '
osascript -e "${the_script}${the_url}"

顺便说一句,使用echo来检查这样的事情是非常误导的. echo告诉你变量中的内容,而不是在命令行上引用变量时要执行的内容.最大的区别是echo在通过bash解析(引用和转义删除等)之后打印它的参数,但是当你说“手动粘贴字符串……工作”时你会说它是你想要的解析之前.如果回显字符串中有引号,则表示bash不会将它们识别为引号并将其删除.相比:

string='"quoted string"'
echo $string          # prints the string with double-quotes around it because bash doesnt't recognize them in a variable
echo "quoted string"  # prints *without* quotes because bash recognizes and removes them

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读