bash Mac:相当于 shell 脚本的 gnome-terminal

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19440007/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:20:43  来源:igfitidea点击:

Mac: gnome-terminal equivalent for shell script

macosbashshellunixterminal

提问by Edwin

I am trying to run a shell script in a MAC terminal. I want to open a new terminal window that will execute the script separate from the program that is running. In Fedora, there is a gnome-terminal command which lets me execute a script in another terminal shell.

我正在尝试在 MAC 终端中运行 shell 脚本。我想打开一个新的终端窗口,该窗口将执行与正在运行的程序分开的脚本。在 Fedora 中,有一个 gnome-terminal 命令可以让我在另一个终端 shell 中执行脚本。

Does anyone know an equivalent on MAX OSX and how to use it?

有谁知道 MAX OSX 上的等价物以及如何使用它?

For example say I have a script crazy.sh and I want to call this from a program that is executing but in a separate terminal from the one which is currently executing the program.

例如,假设我有一个脚本 crazy.sh,我想从一个正在执行的程序中调用它,但是在与当前正在执行该程序的终端不同的终端中。

采纳答案by Digital Trauma

One way to do it is to use an xterm instead of a terminal window:

一种方法是使用 xterm 而不是终端窗口:

xterm -e crazy.sh

If you want the xterm to stay open after the script completes, use the -holdoption to xterm.

如果您希望 xterm 在脚本完成后保持打开状态,请使用-holdxterm 选项。



But if you really need to do this in a terminal, you can do it with applescript:

但是,如果您确实需要在终端中执行此操作,则可以使用 applescript 执行此操作:

tell application "Terminal"
    activate
    tell application "System Events" to keystroke "n" using command down
    repeat while contents of selected tab of window 1 starts with linefeed
        delay 0.1
    end repeat
    do script "crazy.sh" in window 1 -- make sure the path to your script is right
end tell

(Credit to the answer here https://superuser.com/questions/466619/open-new-terminal-tab-and-execute-script)

(归功于这里的答案https://superuser.com/questions/466619/open-new-terminal-tab-and-execute-script

回答by Edwin

I like DigitalTrauma's answer but I found for my use, this worked better

我喜欢 DigitalTrauma 的回答,但我发现它对我有用,效果更好

open -a Terminal.app crazy.sh

Thanks for the answers.

感谢您的回答。