【问题标题】:Bash/Osascript Error: unexpected EOF while looking for matching `)'Bash/Osascript 错误:寻找匹配的 `)' 时出现意外的 EOF
【发布时间】:2018-07-26 15:19:26
【问题描述】:

我正在使用 osascript 拆分字符串(以这种方式工作,而不是使用 bash),并将结果数组分配给 bash 变量,然后继续我的 bash 脚本。我是这样做的:

tempArrayApplications=$(osascript >/dev/null <<EOF
    set oldDelimiters to AppleScript's text item delimiters
    set AppleScript's text item delimiters to "/"
    set theArray to every text item of "$noSplitString"
    set AppleScript's text item delimiters to oldDelimiters
    return theArray
EOF)

但是,命令行返回错误,即它转到文件末尾而没有找到匹配的 ')'。但是,当我没有为 osascript 输出分配 bash 变量时,一切正常,所以我知道 AppleScript 部分没有问题。我运行了shellcheck,它没有检测到任何错误,其他解决方案似乎与未闭合的引号或未转义的字符有关,但我似乎没有这个问题。显然这是由于试图将其分配给 bash 变量,但对于我的生活,我不知道我做错了什么。感谢您的帮助。

【问题讨论】:

标签: bash applescript osascript


【解决方案1】:

您是否考虑过使用 bash 变量 ($noSplitString);将其插入到使用/ 作为分隔符分割文本的AppleScript 中;在 bash 命令中执行此 AppleScript (osascript);然后将其输出(实际上被销毁)存储在另一个 bash 变量($tempArrayApplications)中......?

我倾向于完全删除 AppleScript(无论如何,它的 5 行中有 3 行是多余的),并从 bash 中的字符串创建数组。

所以,鉴于此:

noSplitString="item 1/item 2/item 3"

然后简单地这样做:

IFS='/'
tempArrayApplications=($noSplitString)

现在$tempArrayApplications 将是一个包含三个项目的数组,从索引 0 开始,到索引 2 结束。您可以像这样 echo 数组中的特定元素:

echo "${tempArrayApplications[1]}" # "item 2"

IFS 是 AppleScript text item delimiters 的 bash 等效项。它通常具有默认值⎵\t\n(其中 表示空格字符)。更多内容可以在这篇文章中阅读:Bash IFS: its Definition, Viewing it and Modifying it

【讨论】:

  • 会接受这个作为答案。我对 Applescript 比较熟悉,所以当我可以使用它时,我倾向于这样做,但没有意识到它在 bash 中如此简单。
猜你喜欢
  • 1970-01-01
  • 2019-01-19
  • 1970-01-01
  • 2014-10-09
  • 2020-12-18
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
相关资源
最近更新 更多