3

I'd like to automate Ubuntu through a single-click batch file in Windows.

I can just start ubuntu, but if I add /k cd "path", it just doesn't do anything. It exits with no error.

I'd like to do an automation so that I can click a batch file in Windows and:

  • open Ubuntu in WSL
  • cd to path
  • activate an environment
  • cargo run
  • ... and then leave Ubuntu running in the same instance

... but WSL doesn't seem to behave as I'd expect. I don't understand.

This question is really close, but I have tried it and can't get it to work.

NotTheDr01ds
  • 17,888
  • 1
    Please edit your question and add all the information. It is not clear which version of Windows (10/11) and which version of Ubuntu you are running. Please indicate if you are using WSL (1 or 2?) or a virtual machine like VMWare or VirtualBox to run Ubuntu. Please copy the bat file and paste it directly in your question above and format is as code using the {_} icon above the edit window. Tell us more about the app cargo. Is it a commandline app or an app that needs a GUI for Ubuntu? – user68186 May 27 '22 at 23:20
  • 2
  • to me this is a windows problem as the problem seems to be batch programming IN windows. – Rinzwind May 29 '22 at 00:59
  • 1
    @Rinzwind Not as much as you might think - It's a one-liner batch file to call WSL and launch Ubuntu. The script itself is all Bash/Ubuntu, and is the same that one would execute from the Bash in Ubuntu without WSL involved. – NotTheDr01ds May 29 '22 at 11:48

1 Answers1

2

Essentially, it sounds like you want to script a series of commands in WSL/Ubuntu, but leave the shell open after they are done.

The key steps are:

  • Use the wsl command, not the ubuntu.exe command.
  • Start the shell in which you are going to run the commands with -lic, which will make it both a login shell (to parse your ~/.bash_profile) and an interactive shell (to parse your ~/.bashrc).
  • Separate the commands with semicolons in the wsl arguments.
  • To keep a shell running after, make the final command exec bash

For example:

wsl -e bash -lic "cd /home/myhome/src/project ; source /home/myhome/src/myproject/bin/activate ; cargo run ; exec bash"
NotTheDr01ds
  • 17,888