我一直在尝试找出如何在新的Max OS X Terminal.app窗口中运行bash命令。例如,以下是如何在新的bash进程中运行我的命令:
bash -c "my command here"
但是这种方法会重复使用现有的终端窗口而不是创建一个新的。我希望能够像这样:
Terminal.app -c "my command here"
当然这样做是行不通的。我知道"open -a Terminal.app"命令,但我不知道如何将参数转发到终端,甚至如果我知道该使用哪些参数也不清楚。
我一直在尝试找出如何在新的Max OS X Terminal.app窗口中运行bash命令。例如,以下是如何在新的bash进程中运行我的命令:
bash -c "my command here"
但是这种方法会重复使用现有的终端窗口而不是创建一个新的。我希望能够像这样:
Terminal.app -c "my command here"
当然这样做是行不通的。我知道"open -a Terminal.app"命令,但我不知道如何将参数转发到终端,甚至如果我知道该使用哪些参数也不清楚。
我能想到的一种方法是创建一个 .command 文件,然后像这样运行:
echo echo hello > sayhi.command; chmod +x sayhi.command; open sayhi.command
或者使用 AppleScript:
osascript -e 'tell application "Terminal" to do script "echo hello"'
虽然你要么必须转义很多双引号,要么就不能使用单引号。
osascript -e "tell application \"Terminal\" to do script \"echo '$variable'\""
- tripleee; exit
,例如do script "echo hello; exit"
。您仍然需要单独关闭窗口。 - tripleee部分解决方案:
将想要执行的任务放入一个 shell 脚本中,像这样:
#!/bin/bash
ls
echo "yey!"
别忘了 'chmod +x file
' 使文件可执行。这样你就可以执行它了。
open -a Terminal.app scriptfile
并且它将在一个新窗口中运行。在脚本的末尾添加'bash
'可以使新会话不退出。(虽然你可能需要想办法加载用户的rc文件和其他内容..)
/Users/{username}
。有没有办法让上下文文件夹保持与打开它的父终端窗口相同? - Johnny Oshika我已经尝试了一段时间。这里有一个脚本,可以切换到相同的工作目录,运行命令并关闭终端窗口。
#!/bin/sh
osascript <<END
tell application "Terminal"
do script "cd \"`pwd`\";$1;exit"
end tell
END
tab 1 of window id 7433
的内容。为了抑制这种情况,在 <<END
之前放置 >/dev/null
。 - mklement0如果有人在意的话,这是 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
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
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
TMP_FILE="tmp.command"
可能会出现问题。我建议将其替换为TMP_FILE=$(mktemp "/tmp/command.XXXXXX")
。 - duthen这是我的神奇脚本,如果需要的话会创建一个新的终端窗口,并且切换到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
ttab
"https://www.npmjs.com/package/ttab
npm install ttab -g
ls
ttab ls
code .
ttab -s Grass -t hi -d '~/dev' code .
我的一位同事问我如何同时打开大量 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
通过更新命令列表(它们不一定是ssh调用),您将为每个执行的命令获得一个额外的打开窗口。结尾处的sleep 60
是为了在执行期间保留.command
文件。否则,shell会太快地完成,执行陷阱以删除临时目录(由mktemp创建),而启动的会话没有机会读取文件。
我将这个脚本称为trun。建议将其放在可执行路径的目录中。确保它是可执行的,如下所示:
chmod +x ~/bin/trun
trun tail -f /var/log/system.log
#!/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
$*
将破坏输入中的任何非平凡引用。你应该使用 "$@"
。 - tripleeeopen -b com.apple.terminal
。谢谢。 - Ebru Yener