skrat
skrat

Reputation: 748

Windows bash script to run something from WSL

I am trying to write down a Windows bash script to:

  1. Start Windows Subsystem for Linux (WSL) and
  2. cd within WSL to "../myfolder/"
  3. run ./foo first_parameter second_parameter
  4. Wait until finished and exit WSL
  5. cd within Windows to "../myWinFolder/"
  6. run Foo.exe parameter
  7. wait until finished

This is my attempt:

bash -c "cd ../myFolder/ && ./foo first_parameter second_parameter"
cd ..
cd myWinFolder
START /WAIT Foo.exe parameter

But sadly CMD does not wait for WSL to finish before running the EXE.

Anything I can do?

Upvotes: 1

Views: 1573

Answers (1)

Damo
Damo

Reputation: 6463

I'm happy that the interop between dos and WSL does in fact wait for commands to finish. Try the following:

In a file called runit.bat

echo bat01
bash -c "echo bat02; cd ./bash/; ./runit.sh; echo bat03"
echo bat04

In a sub-folder called ./bash/ paste the following in a file called runit.sh

echo sh01 
sleep 2s 
echo sh02 

When you run runit.bat from within dos you will see a wait of 2 seconds

You have not specified what is inside your ./foo script. I suspect that it is running a task in the background or running something that returns immediately. This can be simulated by putting & after the sleep so that it runs in the background within wsl sleep 2s &. When you do this you see that there is no pause in the execution of the script.

So I would check ./foo maybe add some echo debug statements around inside it and run it from within WSL first to make sure that it does indeed wait until all the commands are finished before it exits.

Upvotes: 1

Related Questions