通过Powershell脚本在WSL中运行bash脚本

问题描述

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

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

但是,在第一个命令打开终端之后,其他两个命令却没有执行

解决方法

.\ubuntu1804.exe本身打开一个 interactive 外壳,PowerShell会同步执行

也就是说,直到您在该交互式外壳中提交exit来终止它为止,控制权都不会返回到PowerShell,因此后续命令-cd test_directorynote server.js-不会不会按照您的预期发送给.\ubuntu1804.exe,而是由PowerShell 运行

相反,您必须通过.\ubuntu1804.exe子命令将要运行的命令传递给run

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

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