运行 bash WSL 脚本通过 powershell 脚本

Run bash script in WSL through powershell script

我正在尝试 运行 启动 WSL (ubuntu1804) 终端的脚本,然后 运行 该终端中的 bash 脚本

.\ubuntu1804.exe;
cd test_directory;
node server.js;

然而,在第一个命令打开终端后,其他两个命令没有执行

.\ubuntu1804.exe 本身打开一个 interactive shell,PowerShell 同步.

执行

也就是说,直到您在那个交互式 shell 中提交 exit 来终止它,控制权不会返回到 PowerShell,因此后续命令 - cd test_directorynote server.js - 不仅没有像您预期的那样发送到 .\ubuntu1804.exe,而且随后由 PowerShell.

运行

相反,您必须通过 run sub-command:

将命令传递给 运行 .\ubuntu1804.exe
.\ubuntu1804.exe run 'cd test_directory; node server.js'

注意:一旦 node 退出,控制权将返回给 PowerShell。