开发者社区> 问答> 正文

从bash脚本打开iTerm2并运行命令

我正在尝试编写脚本以从VS Code在iTerm2中自动启动我的开发服务器。

当我打开VS Code项目时,我希望bash脚本能够:

打开iTerm2( 运行cd .. && cd OtherFolder 运行npm start(这样我的节点服务器将启动) 问题

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

展开
收起
祖安文状元 2020-01-06 15:59:32 2061 0
1 条回答
写回答
取消 提交回答
  • 希望对您有帮助。您可以使用命令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 $@
    
    2020-01-06 15:59:47
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Shell 脚本速查手册 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载