bash - 使用 shell 脚本打开新的 Ubuntu 选项卡,然后在这些新选项卡中运行命令

标签 bash shell ubuntu windows-subsystem-for-linux windows-terminal

我正在使用运行 Ubuntu 的 Windows 终端。我的目标是打开 5 个不同的选项卡,导航到每个选项卡中的特定目录,并在其中启动某些服务。
作为奖励,我想为标签着色并为它们命名以保持整洁,但这并不重要。

#!/usr/bin/env sh

# simple demo script for launching all desired services at once
    
cd /main/directory
sudo service redis-server start
sudo service postgresql start
        
# Note this does not yet work as desired. The tabs are generated, but the command after the color value execute in the current tab, and not the newly created tab.
        
        
cmd.exe /c "wt.exe" --window 0 new-tab --title "APIService" --tabColor "#E74C3C" && cd apiserv/ && npm start
        
cmd.exe /c "wt.exe" --window 0 new-tab --title "AuthServ" --tabColor "#F39C12" && cd authserv/ && npm start
        
cmd.exe /c "wt.exe" --window 0 new-tab --title "MainServ" --tabColor "#F4D03F" && cd mainserv/ && rails s
        
cmd.exe /c "wt.exe" --window 0 new-tab --title "Sidekiq" --tabColor "#3498DB" && cd mainserv/ && sidekiq
    
cmd.exe /c "wt.exe" --window 0 new-tab --title "ClientServ" --tabColor "#27AE60" && cd clientserv/ && yarn start
我喜欢认为我拥有实现所需结果所需的大部分(或至少部分)部分——让服务单独运行没有问题——但显然我缺乏一些 bash 知识来让它们一起正常工作.它将打开一个选项卡,为其着色并(临时)为其命名,但命令的其余部分(例如,'&& cd apiserv/&& npm start')在原始选项卡中执行,并且没有到达任何其他命令。
目前,如果我省略颜色值之后的命令(每次出现,从 && 开始),将创建 5 个选项卡并着色。他们的标题会出现,但很快就会被默认名称覆盖。这并不理想,但它不是主要优先事项。

最佳答案

以下为您提供所需的代码结构:

#!/usr/bin/env sh

wt.exe -w -1 new-tab --title "APIService" --tabColor "#E74C3C" bash -c "cd /tmp\\; read -pEnter" \;\
             new-tab --title "AuthServ"   --tabColor "#F39C12" bash -c "cd /tmp\\; read -pEnter"
# ...
我目前没有要测试的 wsl(所以你可能需要一些适应),但它在 git-bash 中工作。
您可能需要更改 bash -cwsl -e , 或 wsl -e bash -c

关于bash - 使用 shell 脚本打开新的 Ubuntu 选项卡,然后在这些新选项卡中运行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72321619/

相关文章:

bash - 如何在awk中转义gsub中的 "["?

bash - getopts 没有将指定的选项识别为有效

ubuntu - 用于开发的最快 Ubuntu 版本

bash - 条件表达式中的语法错误 : unexpected token `;'

bash - 为什么 bash "test -n"命令在 "! test -z"工作时给出 $@(美元)位置参数的错误结果?

linux - bash shell 自动创建用户 - adduser

c - 上游维护者的工具?用于发布前的测试(Debian等)

bash - 在 Xubuntu 14.10 上的 IFS 之后阅读 Bash 脚本不拆分

git - 如何使 bash 假设所有命令都是 git 命令?

linux - 用于重命名和重新排列文件的脚本 shell