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


#!/bin/bash
#!/usr/bin/python
read -p "Execute script:(y/n) " response
if [ "$response" = "y" ]; then
    echo -e "nnLoading....nn"
    for ((x = 0; x<5; x++))
    do
        echo -e "Open $x terminalnn"
        open -a Terminal.app
    done
fi

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

如果要

从 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 个新的终端窗口,假设您尚未设置为在选项卡中打开新窗口。

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

-n 打开应用程序的新实例,即使该应用程序已在运行。

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

最新更新