首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从bash脚本打开iTerm2 &运行命令

从bash脚本打开iTerm2 &运行命令
EN

Stack Overflow用户
提问于 2019-07-03 03:45:06
回答 2查看 5.6K关注 0票数 0

摘要

我正在尝试编写一个脚本,以便从VS代码中自动在iTerm2中启动我的开发服务器。

当我打开VS代码项目时,我希望bash脚本:

  1. 开放iTerm2 (
  2. 播放cd ..。&& cd OtherFolder
  3. 运行npm (因此我的节点服务器将启动)

问题

我知道如何打开iTerm2,但我不知道如何使我编写的bash脚本在iTerm2中运行#2和#3中的命令,因为我需要从VS代码终端运行bash脚本,然后打开iTerm2。

EN

回答 2

Stack Overflow用户

发布于 2019-07-03 04:15:41

希望它能帮到你。您可以使用命令iTerm2而不是iTerm。

代码语言:javascript
复制
#!/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 $@
票数 2
EN

Stack Overflow用户

发布于 2019-11-14 20:39:56

无耻的插头,但你可以尝试使用我为自己建立的小程序,这将很容易让你做你想做的事情。

它将允许您指定这样的配置文件:

代码语言:javascript
复制
{
  "tabs": [
    {
      "commands": ["cd /to/other/folder", "cd /to/project && npm start"]
    }
  ]
}

做你想做的事-)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56862644

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档