使用bash脚本打开多个终端窗口mac

时间:2018-02-02 21:38:35

标签: linux bash macos shell unix

#!/bin/bash
#!/usr/bin/python

read -p "Execute script:(y/n) " response
if [ "$response" = "y" ]; then
    echo -e "\n\nLoading....\n\n"

    for ((x = 0; x<5; x++))
    do
        echo -e "Open $x terminal\n\n"
        open -a Terminal.app
    done
fi

这只会打开一个新的终端窗口。如何打开10个新的终端窗口?

2 个答案:

答案 0 :(得分:1)

如果要从bash脚本(或从命令行)打开10个新的终端窗口,请使用以下命令:

osascript -e 'tell application "Terminal"' -e 'repeat 10 times' -e 'do script ""' -e 'end repeat' -e 'end tell'

或者集成到现有代码中,尽管已重新编码:

#!/bin/bash

shopt -s nocasematch
read -p " Execute script? (y/n): " response
if [[ $response == y ]]; then
    printf " Loading....\\n"
    for ((x = 0; x<10; x++)); do
        printf " Open %s Terminal\\n" $x
        osascript -e 'tell application "Terminal" to do script ""' >/dev/null
    done
fi
shopt -u nocasematch

它的输出是:

$ ./codetest
 Execute script? (y/n): y
 Loading....
 Open 0 Terminal
 Open 1 Terminal
 Open 2 Terminal
 Open 3 Terminal
 Open 4 Terminal
 Open 5 Terminal
 Open 6 Terminal
 Open 7 Terminal
 Open 8 Terminal
 Open 9 Terminal
$ 

显示10个新的终端窗口,假设您没有设置在标签页中打开新窗口。

答案 1 :(得分:0)

您需要为open命令提供-n选项

-n打开一个新的应用程序实例,即使其中一个已经在运行。

for ((x = 0; x<5; x++)) do
    echo -e "Open $x terminal\n\n"
    open -na Terminal.app
done