bash 在新的 Mac OS X 终端窗口中运行命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/989349/
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
Running a command in a new Mac OS X Terminal window
提问by Walt D
I've been trying to figure out how to run a bash command in a new Max OS X Terminal.app window. As, an example, here's how I would run my command in a new bash process:
我一直试图弄清楚如何在新的 Max OS X Terminal.app 窗口中运行 bash 命令。例如,以下是我在新的 bash 进程中运行命令的方式:
bash -c "my command here"
But this reuses the existing terminal window instead of creating a new one. I want something like:
但这会重用现有的终端窗口,而不是创建一个新窗口。我想要这样的东西:
Terminal.app -c "my command here"
But of course this doesn't work. I am aware of the "open -a Terminal.app" command, but I don't see how to forward arguments to the terminal, or even if I did what arguments to use.
但这当然行不通。我知道“open -a Terminal.app”命令,但我不知道如何将参数转发到终端,或者即使我做了要使用的参数。
回答by cobbal
one way I can think to do it off the top of my head is to create a .command file and run it like so:
我可以想到的一种方法是创建一个 .command 文件并像这样运行它:
echo echo hello > sayhi.command; chmod +x sayhi.command; open sayhi.command
or use applescript:
或使用applescript:
osascript -e 'tell application "Terminal" to do script "echo hello"'
although you'll either have to escape a lot of double quotes or not be able to use single quotes
虽然你要么不得不转义很多双引号,要么不能使用单引号
回答by 0scar
Partial solution:
部分解决方案:
Put the things you want done in a shell-script, like so
把你想做的事情放在一个 shell 脚本中,就像这样
#!/bin/bash
ls
echo "yey!"
And don't forget to 'chmod +x file
' to make it executable. Then you can
并且不要忘记使用 ' chmod +x file
' 使其可执行。然后你可以
open -a Terminal.app scriptfile
and it will run in a new window. Add 'bash
' at the end of the script to keep the new session from exiting. (Although you might have to figure out how to load the users rc-files and stuff..)
它将在新窗口中运行。bash
在脚本末尾添加“ ”以防止新会话退出。(尽管您可能需要弄清楚如何加载用户的 rc 文件和其他东西..)
回答by 0scar
I've been trying to do this for a while. Here is a script that changes to the same working directory, runs the command, and closes the terminal window.
我一直在尝试这样做。这是一个更改到同一工作目录、运行命令并关闭终端窗口的脚本。
#!/bin/sh
osascript <<END
tell application "Terminal"
do script "cd \"`pwd`\";;exit"
end tell
END
回答by Al Chou
In case anyone cares, here's an equivalent for iTerm:
如果有人关心,这里有一个 iTerm 的等价物:
#!/bin/sh
osascript <<END
tell application "iTerm"
tell the first terminal
launch session "Default Session"
tell the last session
write text "cd \"`pwd`\";;exit"
end tell
end tell
end tell
END
回答by Dave Butler
I made a function version of Oscar's answer, this one also copies the environment and changes to the appropriate directory
我做了一个Oscar的回答的函数版本,这个也复制了环境和修改到相应的目录
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"
}
You can use it like this:
你可以这样使用它:
new_window my command here
or
或者
new_window ssh example.com
回答by Dave Butler
Here's yet another take on it (also using AppleScript):
这是另一种看法(也使用 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'
see: codesnippets.joyent.com/posts/show/1516
请参阅:codesnippets.joyent.com/posts/show/1516
回答by w00t
Here's my awesome script, it creates a new terminal window if needed and switches to the directory Finder is in if Finder is frontmost. It has all the machinery you need to run commands.
这是我很棒的脚本,如果需要,它会创建一个新的终端窗口,如果 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
回答by Mark
A colleague asked me how to open A LOT of ssh sessions at once. I used cobbal's answer to write this script:
一位同事问我如何一次打开很多 ssh 会话。我用 cobbal 的回答写了这个脚本:
tmpdir=$( mktemp -d )
trap '$DEBUG rm -rf $tmpdir ' EXIT
index=1
{
cat <<COMMANDS
ssh user1@host1
ssh user2@host2
COMMANDS
} | while read command
do
COMMAND_FILE=$tmpdir/$index.command
index=$(( index + 1 ))
echo $command > $COMMAND_FILE
chmod +x $COMMAND_FILE
open $COMMAND_FILE
done
sleep 60
By updating the list of commands ( they don't have to be ssh invocations ), you will get an additional open window for every command executed. The sleep 60
at the end is there to keep the .command
files around while they are being executed. Otherwise, the shell completes too quickly, executing the trap to delete the temp directory ( created by mktemp ) before the launched sessions have an opportunity to read the files.
通过更新命令列表(它们不必是 ssh 调用),您将为每个执行的命令获得一个额外的打开窗口。将sleep 60
在年底有保持.command
正在执行,而他们周围的文件。否则,shell 完成得太快,在启动的会话有机会读取文件之前执行陷阱以删除临时目录(由 mktemp 创建)。
回答by ayaz
You could also invoke the new commandfeature of Terminal by pressing the Shift + ? + N
key combination. The command you put into the box will be run in a new Terminal window.
您还可以通过按组合键来调用终端的新命令功能Shift + ? + N
。您放入框中的命令将在新的终端窗口中运行。
回答by Roger
I call this script trun. I suggest putting it in a directory in your executable path. Make sure it is executable like this:
我称这个脚本为 trun。我建议将它放在可执行路径中的目录中。确保它可以像这样执行:
chmod +x ~/bin/trun
Then you can run commands in a new window by just adding trun before them, like this:
然后你可以在新窗口中运行命令,只需在它们之前添加 trun ,就像这样:
trun tail -f /var/log/system.log
Here's the script. It does some fancy things like pass your arguments, change the title bar, clear the screen to remove shell startup clutter, remove its file when its done. By using a unique file for each new window it can be used to create many windows at the same time.
这是脚本。它做了一些奇特的事情,比如传递你的参数,改变标题栏,清除屏幕以消除 shell 启动混乱,完成后删除它的文件。通过为每个新窗口使用唯一的文件,它可用于同时创建多个窗口。
#!/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 "3]0;'$*'##代码##7"' >>$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