3

I'm using Windows Terminal running Ubuntu. My goal is to open 5 different tabs, navigate to specific directories within each of those tabs, and start certain services therein.

As a bonus, I'd like to colour the tabs and title them for tidiness' sake, but this is not vital.

#!/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

I like to think I have most of (or at least some of) the pieces I need to achieve my desired result - there's no issue with getting the services running individually - but obviously I'm lacking some bash knowledge to get them working properly together. It'll open one tab, colour and (temporarily) name it, but the rest of the command (eg. '&& cd apiserv/ && npm start') executes in the original tab, and none of the other commands are reached.

At present, if I omit the commands after the colour value (at each occurrence, starting from the &&), the 5 tabs will be created and coloured. Their titles will appear, but quickly be overwritten by the default name. This isn't ideal, but it's not a main priority.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • When you type `cmd ... && command`, it runs the `cmd ...` first, and if it ran ok, it will then run the `command`. It does not imply that the command runs in cmd. I bash for exemple, you would do `bash -c command` which runs command inside the bash. I do not know the switches for cmd, but hopefully something similar exists. – Nic3500 May 20 '22 at 17:14
  • Look at https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/cmd, it shows how to run many commands using the `/c` switch. – Nic3500 May 20 '22 at 17:15

1 Answers1

0

Following gives you the code structure you need :

#!/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"
# ...

I don't have wsl to test at the moment (so you may need some adaptation), but it worked in git-bash.

You might need to change bash -c to wsl -e, or wsl -e bash -c

Philippe
  • 20,025
  • 2
  • 23
  • 32