基于变量的OSX脚本:打开一个新的iTerm窗口并运行命令

3
我试图使用这里所示的iTerm示例来回答另一个问题。
基本上,我有一个包含大约20个文本文件的列表,这些文件表示来自不同服务器的报告。我希望在它们所在的目录中读取每个文件名,并从中构建一个存在于commands目录中的shell命令,然后打开一个新的iTerm窗口,再执行我构建的shell脚本。
我不想在一个窗口中一个接一个地运行它们,我希望它们各自在自己的窗口中执行以加快处理速度。
这是我的代码,我可以很愉快地构建出shell脚本名称并将其存储在foo中,而且似乎我也可以顺利地打开新的iTerm窗口,但是我在让它接受$foo作为要运行的命令方面遇到了麻烦。
#!/bin/sh
FILES=/Volumes/reporter/uplod/lists/*
# eg each filename is of the type <path>/X07QXL29.txt
for f in $FILES
do
  foo="del"
  foo+=${f:32:1}
  foo+=${f:36:2}
  foo+=".sh"
  # foo is now for example del729.sh using the above comment as the filename
  # foo is the command I will want to run in its own new window
  osascript <<END
    tell application "iTerm"
        tell the first terminal
        tell myterm
            launch session "Default Session"
            tell the last session
                write text "$foo"
                write text "\n"
            end tell
        end tell
    end tell
  END
done

我收到的错误是:deltrash.sh: 第22行:语法错误:意外的文件结尾。
有人能给我一个指针吗?

1
你是为了让程序运行更快才在新窗口中执行每个进程吗?如果是这样,你可以通过在每个命令的末尾添加一个“&”符号来在单个窗口中并行运行所有进程。或者你可以使用GNU Parallel-k开关将它们全部并行运行,以保持输出分离和有序。 - Mark Setchell
是的,我希望能够在每个窗口中看到命令的输出,因此让它们全部显示在单个窗口中对我来说可能会有问题。 - user3169632
1个回答

3

看一下iTerm的applescript 例子,这样做应该可以。基本上你需要把myterm变量设置为新的终端实例。同时确保在行首放置END标记。它在你的脚本中未被检测到,因此出现了unexpected end of file错误。

#!/bin/sh
FILES=/Volumes/reporter/uplod/lists/*
# eg each filename is of the type <path>/X07QXL29.txt
for f in $FILES
do
  foo="del"
  foo+=${f:32:1}
  foo+=${f:36:2}
  foo+=".sh"
  # foo is now for example del729.sh using the above comment as the filename
  # foo is the command I will want to run in its own new window
  osascript <<END
    tell application "iTerm"
        set myterm to (make new terminal)
        tell myterm
            launch session "Default Session"
            tell the last session
                write text "$foo"
                write text "\n"
            end tell
        end tell
    end tell
END
done

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接