【发布时间】:2019-05-31 04:12:10
【问题描述】:
我正在尝试让 applescript 与作为我代码的一部分的 bash for 循环进行交互,以避免手动列出每个主机并为 hosts2.txt 中找到的每个主机执行单独的告诉/结束告诉块文件。
脚本的目的是在我的 Mac 上打开一个新的终端选项卡,并在每个新终端中自动启动“screen -r $HOST”,直到 hosts2.txt 文档中的主机列表结束。每个主机都列在自己的行上。
我尝试了一个全包 for 循环,没有下面显示的 applescript“重复 2 次”“结束重复”代码。它重复了 2 次,因为文本文档中仅列出了 2 个主机用于测试目的。每次我都有错误输出。
#!/bin/bash
for HOST in `cat ~/bin/hosts2.txt`
do echo $HOST
osascript -e 'repeat 2 times
tell application "Terminal" activate
tell application "System Events" to keystroke "t" using [command down]
tell application "Terminal" to activate
set host to $HOST
tell application "Terminal"
do shell script "screen -r " & host in front window
end tell
end repeat'
done
我期望发生的代码是使用 screen -r 为每个主机打开新终端选项卡的代码。错误输出低于此行。
dev
44:52: syntax error: Expected end of line but found command name. (-2741)
pulsar
44:52: syntax error: Expected end of line but found command name. (-2741)
【问题讨论】:
-
非常好的问题,提供了很好的信息,做得好,谢谢。问题:您运行的是什么版本的 macOS?另外,如果您不介意,请发布您的
~/bin/hosts2.txt文件的内容。 -
hosts2.txt 仅包含以下内容: dev pulsar 各占一行。
-
忘记回答其他问题。我正在使用 Mojave。
标签: bash applescript