使用单个 bash 行在多个终端窗口中运行多个命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3232305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run multiple commands in multiple Terminal windows with a single bash line
提问by Trevor Burnham
Here's an interesting problem: Using the AppleScript methodto launch a new command in a Terminal window fails if Terminal is "busy"; more precisely, it will open a new window but fail to run the command. For example, try copy+pasting this line in:
这是一个有趣的问题:如果终端“忙”,则使用AppleScript 方法在终端窗口中启动新命令将失败;更准确地说,它将打开一个新窗口,但无法运行该命令。例如,尝试复制+粘贴这一行:
osascript -e 'tell application "Terminal" to do script "foo"'; osascript -e 'tell application "Terminal" to do script "bar"'; osascript -e 'tell application "Terminal" to do script "baz"';
When I run this (I'm on Snow Leopard), I get three windows: In the first, the command foois run, as expected; but in the other two, I just get an empty prompt. And I get empty prompts in all three windows if I've just pasted the command in; apparently Terminal is still busy processing the "paste" operation.
当我运行它时(我在 Snow Leopard 上),我得到三个窗口:在第一个窗口中,命令foo按预期运行;但在另外两个中,我只得到一个空提示。如果我刚刚粘贴了命令,我在所有三个窗口中都会得到空提示;显然终端仍在忙于处理“粘贴”操作。
Now, AppleScript might not be the best way of doing this, but I really want to write a script that launches a bunch of Terminal windows, in order, and runs a command in each. Maybe the best way to do that is with a bash script. As long as I can create an alias to it, I'm happy. So how to do this?
现在,AppleScript 可能不是这样做的最佳方式,但我真的想编写一个脚本,按顺序启动一堆终端窗口,并在每个窗口中运行一个命令。也许最好的方法是使用 bash 脚本。只要我能为它创建一个别名,我就很高兴。那么如何做到这一点呢?
回答by mcgrailm
what are the actual scripts you are running
您正在运行的实际脚本是什么
I modified your code alittle to test it
我稍微修改了你的代码来测试它
osascript -e 'tell application "Terminal" to do script "cd Desktop"'; osascript -e 'tell application "Terminal" to do script "cd .."'; osascript -e 'tell application "Terminal" to do script "ls"';
and this seems to work fine
这似乎工作正常

