如何用 shell 开启一个新的 Terminal 或者 iTerm,并在其中执行指定命令?? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
FatGhosta
V2EX    Apple

如何用 shell 开启个新的 Terminal 或者 iTerm,并在其中执行指定命令??

  •  
  •   FatGhosta 2015-08-28 18:15:22 +08:00 8805 次点击
    这是一个创建于 3702 天前的主题,其中的信息可能已经有所发展或是发生改变。

    问题的后半句是关键!!

    我想在 Terminal 里,用 shell 命令再调用出一个 Terminal ,再在这个刚启动的执行刚才 shell 命令中带的命令???
    我现在用exec /Applications/iTerm.app/Contents/MacOS/iTerm,在 iTerm 中启动新的 iTerm 。我想执行类似 exec /Applications/iTerm.app/Contents/MacOS/iTerm | xargs ls, 在新打开的 iTerm 中执行ls命令。这个命令显然不行。但是有没有这种命令??

    或者通过 swift 可以?

    12 条回复    2018-03-21 11:16:53 +08:00
    fatestigma
        1
    fatestigma  
       2015-08-28 18:44:20 +08:00   1
    你说的方法我不太会。
    但是 AppleScript 可以实现。对应的 shell 是 osascript -e;
    比如 iTerm 的:
    ```AppleScript
    osascript -e 'tell application "iTerm"
    set _term to (make new terminal )
    tell _term
    launch session "Default"
    set _session to current session
    end tell
    tell _session
    write text "ls"
    end tell
    end tell'
    ```
    fatestigma
        2
    fatestigma  
       2015-08-28 18:46:12 +08:00   1
    缩进怎么不见了。。另外倒数第三行那个双引号里的可以直接修改成你要执行的。
    ```
    osascript -e 'tell application "iTerm"
    set _term to (make new terminal )
    tell _term
    launch session "Default"
    set _session to current session
    end tell
    tell _session
    write text "ls"
    end tell
    end tell'
    ```
    FatGhosta
        3
    FatGhosta  
    OP
       2015-08-28 18:55:10 +08:00
    @fatestigma Mac 小白,不知道还有 AppleScript 这么个神器。。。
    pityonline
        4
    pityonline  
       2015-08-28 19:17:18 +08:00 via iPhone
    其实你需要的是 tmux ,只在一个终端里全搞定!
    ooonme
        5
    ooonme  
       2015-08-28 19:28:09 +08:00
    open -a Terminal
    ooome
        6
    ooonme  
       2015-08-28 19:28:41 +08:00
    osx 里的
    ooonme
        7
    ooonme  
       2015-08-28 19:31:29 +08:00
    如果本身在 Termianl 执行 open -n -a Terminal
    FatGhosta
        8
    FatGhosta  
    OP
       2015-08-29 13:05:15 +08:00
    @ooonme exec 也可以,最重要的是能不能这个命令把在新 Terminal 中的要输入的命令带上。
    Creolophus
        9
    Creolophus  
       2017-07-28 11:33:15 +08:00
    @fatestigma set myterm to (make new terminal)
    Creolophus
        10
    Creolophus  
       2017-07-28 11:34:10 +08:00
    @fatestigma
    tell application "iTerm"
    activate
    set myterm to (make new terminal)
    end tell

    error "The variable terminal is not defined." number -2753 from "terminal"
    terminal 未定义是怎么回事啊
    fatestigma
        11
    fatestigma  
       2017-07-28 16:29:27 +08:00
    @Creolophus /这是一个创建于 699 天前的主题,其中的信息可能已经有所发展或是发生改变。/
    简单来说,就是 iTerm2 3.0 的 AppleScript 字典变了,terminal 关键字没有了。

    tell application "iTerm"
    create window with default profile
    tell current session of current window to write text "ls"
    end tell
    noogler67
        12
    noogler67  
       2018-03-21 11:16:53 +08:00
    open -a Terminal bash.sh
    open -a iTerm bash.sh
    如果要新窗口 -n
    注意 bash.sh 需要有执行权限
    提前执行 sudo chmod +x bash.sh
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1203 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 23ms UTC 17:39 PVG 01:39 LAX 10:39 JFK 13:39
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86