我正在使用运行 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 -c
至wsl -e
, 或 wsl -e bash -c
关于bash - 使用 shell 脚本打开新的 Ubuntu 选项卡,然后在这些新选项卡中运行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72321619/