9

如何从 Visual Studio 代码运行 Windows Bash 作为运行任务?

以下是我的许多尝试中的一些tasks.json

{
    "version": "0.1.0",
    "command": "cmd",
    "isShellCommand": true,
    "args": [ "RunShellScript.bat" ],
    "showOutput": "always"
}

RunShellScript.bat 只有这一行:bash myShellScript.sh. 如果您只是从开始打开 ​​cmd 并键入该行,它将执行。如果您也只需双击该文件,它也可以完美运行。但是,当从 VSCode 启动时,这个输出会一直挂起,直到我终止它。

尝试2:

{
    "version": "0.1.0",
    "command": "cmd",
    "isShellCommand": true,
    "args": [ "bash myShellScript.sh" ],
    "showOutput": "always"
}

这也挂起,就像上面一样。

尝试3:

{
    "version": "0.1.0",
    "command": "C:/Windows/System32/bash",
    "isShellCommand": false,
    "args": [ "myShellScript.sh" ],
    "showOutput": "always"
}

这给了我:

Failed to launch external program C:/Windows/System32/bash myShellScript.sh
spawn C:/Windows/System32/bash ENOENT

在某些情况下,我还尝试将"isShellCommand"变量设置为 true 和 false,但无济于事。

有谁知道如何做到这一点?

4

3 回答 3

7

我也想看看我怎么能做同样的事情。

andlrc - 我已经尝试了你的两个选项,但似乎都没有成功。问题似乎在于 bash 在 Visual Studio Code 运行的任何上下文中都无法识别。我尝试了各种方法:

  • “命令”:“C:/Windows/System32/bash.exe”
  • “命令”:“重击”
  • “命令”:“bash.exe”

如果我得到一些工作,我会继续尝试并回帖。

编辑:明白了。归功于这个人 - https://aigeec.com/using-windows-10-anniversary-bash-with-visual-studio-code/

总之,归结为我使用的是 64 位操作系统,这意味着 bash.exe 位于 C:\Windows\System32 中。一旦我按照博客文章的建议将 bash.exe 复制到 SysWOW64 中,Visual Studio Code 的默认构建操作 (Ctrl+Shift+B) 就会按照您的预期运行。

这似乎是一个 hacky 解决方法 - 可能值得向 MSFT 提出,看看我们是否可以做些什么。

编辑:这是我要求的tasks.json:

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "0.1.0",
  "command": "bash",
  "isShellCommand": true,
  "args": [ "./gulp.sh" ],
  "showOutput": "always",
  "tasks": [{
    "taskName": "scripts",
    "isBuildCommand": true,
    "problemMatcher": "$gulp-tsc",
    "isWatching": true
  }]

}

gulp.sh 只包含一个命令;'gulp' :-) 但你基本上可以把任何你想要的东西放在那里,我想你可以有几个 .sh 脚本来满足不同的任务磁带

作为对使用 Sysnative 的回应,不幸的是,它似乎不理解这一点,并且构建任务没有运行。

于 2016-05-16T12:59:50.933 回答
3

在我新安装的 Windows 10 机器上bash,这对我来说很好:

{
    "version": "0.1.0",
    "command": "bash",
    "isShellCommand": true,
    "args": [ "shellscript.sh" ],
    "showOutput": "always"
}

shellscript.sh as 的 shebang 版本command对我不起作用。

您还可以使用bash -c "<command>"来运行任意命令:

{
    "version": "0.1.0",
    "command": "bash",
    "isShellCommand": true,
    "args": ["-c", "echo \"test\""],
    "showOutput": "always"
}

在此处输入图像描述

于 2016-05-22T18:53:15.657 回答
1

尝试:

{
    "version": "0.1.0",
    "command": "bash",
    "isShellCommand": true,
    "args": [ "myShellScript.sh" ],
    "showOutput": "always"
}

或者,您可以在文件中添加 shebang:

#!/bin/bash

然后写入脚本的路径:

{
    "version": "0.1.0",
    "command": "/path/to/script.sh",
    "isShellCommand": true,
    "args": [],
    "showOutput": "always"
}
于 2016-05-16T08:05:38.247 回答