在新的Mac OS X Terminal窗口中运行命令

我一直想弄清楚如何在新的Max OS X Terminal.app窗口中运行bash命令。 作为一个例子,下面是我将如何在新的bash过程中运行我的命令:

bash -c "my command here" 

但是,这重用了现有的terminal窗口,而不是创build一个新窗口。 我想要的东西是:

 Terminal.app -c "my command here" 

但是,这当然不起作用。 我知道“打开-a Terminal.app”命令,但我不知道如何将参数转发到terminal,或者即使我做了什么参数使用。

谢谢!

我能想到的一个办法就是创build一个.command文件,像下面这样运行它:

 echo echo hello > sayhi.command; chmod +x sayhi.command; open sayhi.command 

或使用苹果:

 osascript -e 'tell application "Terminal" to do script "echo hello"' 

尽pipe你要么必须避免大量的双引号,要么不能使用单引号

部分解决scheme:

把你想做的东西放在一个shell脚本中,像这样

 #!/bin/bash ls echo "yey!" 

不要忘记' chmod +x file '使其可执行。 然后你可以

 open -a Terminal.app scriptfile 

它会在新窗口中运行。 在脚本的末尾添加“ bash ”以防止新的会话退出。 (虽然你可能要弄清楚如何加载用户rc文件和东西..)

我一直在试图做一段时间。 这是一个脚本,它改变到相同的工作目录,运行命令并closuresterminal窗口。

 #!/bin/sh osascript <<END tell application "Terminal" do script "cd \"`pwd`\";$1;exit" end tell END 

如果有人在意,这里是iTerm的等价物:

 #!/bin/sh osascript <<END tell application "iTerm" tell the first terminal launch session "Default Session" tell the last session write text "cd \"`pwd`\";$1;exit" end tell end tell end tell END 

这是我的真棒脚本,如果需要的话,它会创build一个新的terminal窗口,如果Finder是最前面的,则切换到Finder所在的目录。 它拥有运行命令所需的所有机器。

 on run -- Figure out if we want to do the cd (doIt) -- Figure out what the path is and quote it (myPath) try tell application "Finder" to set doIt to frontmost set myPath to finder_path() if myPath is equal to "" then set doIt to false else set myPath to quote_for_bash(myPath) end if on error set doIt to false end try -- Figure out if we need to open a window -- If Terminal was not running, one will be opened automatically tell application "System Events" to set isRunning to (exists process "Terminal") tell application "Terminal" -- Open a new window if isRunning then do script "" activate -- cd to the path if doIt then -- We need to delay, terminal ignores the second do script otherwise delay 0.3 do script " cd " & myPath in front window end if end tell end run on finder_path() try tell application "Finder" to set the source_folder to (folder of the front window) as alias set thePath to (POSIX path of the source_folder as string) on error -- no open folder windows set thePath to "" end try return thePath end finder_path -- This simply quotes all occurrences of ' and puts the whole thing between 's on quote_for_bash(theString) set oldDelims to AppleScript's text item delimiters set AppleScript's text item delimiters to "'" set the parsedList to every text item of theString set AppleScript's text item delimiters to "'\\''" set theString to the parsedList as string set AppleScript's text item delimiters to oldDelims return "'" & theString & "'" end quote_for_bash 

我做了一个奥斯卡的答案的function版本,这一个也复制环境,并改变到适当的目录

 function new_window { TMP_FILE=$(mktemp "/tmp/command.XXXXXX") echo "#!/usr/bin/env bash" > $TMP_FILE # Copy over environment (including functions), but filter out readonly stuff set | grep -v "\(BASH_VERSINFO\|EUID\|PPID\|SHELLOPTS\|UID\)" >> $TMP_FILE # Copy over exported envrionment export -p >> $TMP_FILE # Change to directory echo "cd $(pwd)" >> $TMP_FILE # Copy over target command line echo "$@" >> $TMP_FILE chmod +x "$TMP_FILE" open -b com.apple.terminal "$TMP_FILE" sleep .1 # Wait for terminal to start rm "$TMP_FILE" } 

你可以像这样使用它:

 new_window my command here 

要么

 new_window ssh example.com 

这里还有另一个(也使用AppleScript):

 function newincmd() { declare args # escape single & double quotes args="${@//\'/\'}" args="${args//\"/\\\"}" printf "%s" "${args}" | /usr/bin/pbcopy #printf "%q" "${args}" | /usr/bin/pbcopy /usr/bin/open -a Terminal /usr/bin/osascript -e 'tell application "Terminal" to do script with command "/usr/bin/clear; eval \"$(/usr/bin/pbpaste)\""' return 0 } newincmd ls newincmd echo "hello \" world" newincmd echo $'hello \' world' 

请参阅:codesnippets.joyent.com/posts/show/1516

您也可以通过按Shift + ⌘ + N组合键来调用terminal的新命令function。 您放入框中的命令将在新的terminal窗口中运行。

我把这个脚本叫做trun。 我build议把它放在你的可执行文件path中的一个目录中。 确保它是可执行的,像这样:

 chmod +x ~/bin/trun 

那么你可以在新窗口中运行命令,只需在它们之前添加trun,如下所示:

 trun tail -f /var/log/system.log 

这是脚本。 它做一些奇特的事情,如传递你的参数,改变标题栏,清除屏幕,以删除shell启动混乱,删除它的文件,当它完成。 通过为每个新窗口使用独特的文件,它可以用来同时创build多个窗口。

 #!/bin/bash # make this file executable with chmod +x trun # create a unique file in /tmp trun_cmd=`mktemp` # make it cd back to where we are now echo "cd `pwd`" >$trun_cmd # make the title bar contain the command being run echo 'echo -n -e "\033]0;'$*'\007"' >>$trun_cmd # clear window echo clear >>$trun_cmd # the shell command to execute echo $* >>$trun_cmd # make the command remove itself echo rm $trun_cmd >>$trun_cmd # make the file executable chmod +x $trun_cmd # open it in Terminal to run it in a new Terminal window open -b com.apple.terminal $trun_cmd 
 open `which $command` 

适用于简单的情况。