[英]Open iTerm2 from bash script & Run commands
概括
我正在嘗試編寫一個腳本來從 VS Code 在 iTerm2 中自動啟動我的開發服務器。
當我打開我的 VS Code 項目時,我希望我的 bash 腳本:
問題
我知道如何打開 iTerm2,但我不知道如何制作我編寫的 bash 腳本,然后在 iTerm2 中運行 #2 和 #3 中的命令,因為我需要從 VS Code 終端運行 bash 腳本,然后打開 iTerm2 .
希望對你有幫助。 您可以使用命令 iTerm2 而不是 iTerm。
#!/bin/bash
#
# Open new iTerm window from the command line
#
# Usage:
# iterm Opens the current directory in a new iTerm window
# iterm [PATH] Open PATH in a new iTerm window
# iterm [CMD] Open a new iTerm window and execute CMD
# iterm [PATH] [CMD] ... You can prob'ly guess
#
# Example:
# iterm ~/Code/HelloWorld ./setup.sh
#
# References:
# iTerm AppleScript Examples:
# https://gitlab.com/gnachman/iterm2/wikis/Applescript
#
# Credit:
# Inspired by tab.bash by @bobthecow
# link: https://gist.github.com/bobthecow/757788
#
# OSX only
[ `uname -s` != "Darwin" ] && return
function iterm () {
local cmd=""
local wd="$PWD"
local args="$@"
if [ -d "$1" ]; then
wd="$1"
args="${@:2}"
fi
if [ -n "$args" ]; then
# echo $args
cmd="; $args"
fi
osascript &>/dev/null <<EOF
tell application "iTerm"
activate
set term to (make new terminal)
tell term
launch session "Default Session"
tell the last session
delay 1
write text "cd $wd$cmd"
end
end
end tell
EOF
}
iterm $@
無恥的插件,但是您可以嘗試使用我為自己構建的這個小程序,它可以輕松地讓您做您想做的事。
它會讓你指定一個這樣的配置文件:
{
"tabs": [
{
"commands": ["cd /to/other/folder", "cd /to/project && npm start"]
}
]
}
那會做你想做的:-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.