새 Mac OS X 터미널 창에서 명령 실행
새로운 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"'
많은 큰 따옴표를 이스케이프해야하거나 작은 따옴표를 사용할 수 없더라도
부분 솔루션 :
원하는 것을 쉘 스크립트에 넣으십시오.
#!/bin/bash
ls
echo "yey!"
그리고 ' chmod +x file'을 (를) 실행 가능하게 만드는 것을 잊지 마십시오 . 그런 다음
open -a Terminal.app scriptfile
새 창에서 실행됩니다. bash새 세션이 종료되지 않도록 스크립트 끝에 ' '를 추가하십시오 . (사용자의 rc 파일 등을로드하는 방법을 알아 내야 할 수도 있습니다.)
나는 이것을 한동안 시도 해왔다. 다음은 동일한 작업 디렉토리로 변경하고 명령을 실행하고 터미널 창을 닫는 스크립트입니다.
#!/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
나는 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"
}
다음과 같이 사용할 수 있습니다.
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
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.
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
You could also invoke the new command feature of Terminal by pressing the Shift + ⌘ + N key combination. The command you put into the box will be run in a new Terminal window.
I call this script trun. I suggest putting it in a directory in your executable path. Make sure it is executable like this:
chmod +x ~/bin/trun
Then you can run commands in a new window by just adding trun before them, like this:
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.
#!/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
참고URL : https://stackoverflow.com/questions/989349/running-a-command-in-a-new-mac-os-x-terminal-window
'Programing' 카테고리의 다른 글
| Gradle에서 현재 OS를 감지하는 방법 (0) | 2020.09.21 |
|---|---|
| convert_tz는 null을 반환합니다. (0) | 2020.09.21 |
| JavaScript를 사용하여 URL 유효성 검사 시도 (0) | 2020.09.21 |
| 업데이트 후 후크를 사용하여 다른 저장소에서 'git pull'을 실행할 때 "치명적 : git 저장소가 아님 : '.'"발생 (0) | 2020.09.21 |
| https 웹 서버용 .pem 파일을 만드는 방법 (0) | 2020.09.21 |