【问题标题】:VS Code: Running Tasks with WSL/BashVS Code:使用 WSL/Bash 运行任务
【发布时间】:2018-11-03 22:38:26
【问题描述】:

WSL 使获取 gcc 和其他 Linux 工具变得容易。您可以使用脚本快速配置 WSL 开发环境,因此如果您搞砸了,您可以快速销毁它并以最低成本重新开始。因此,我想在 WSL 而不是 Windows 本身上运行任务。

例如,在 Sublime3 中,我可以使用 GCC 的 WSL 安装来构建 C 代码,只需使用以下代码创建一个新的构建系统:

{
  "cmd" : ["bash", "-c", "gcc ${file_name} -o ${file_base_name} && ./${file_base_name}"],
  "shell": true,
  "working_dir": "${file_path}",
}

到目前为止,我一直在试图弄清楚如何在 VS Code 上复制它,但没有成功。似乎至少有另一个人设法得到了这个working,所以应该是可能的。

似乎正确的方法是配置一个tasks.json文件。相关行似乎是:

"command": "bash.exe",
    "args": ["-c", "'insert code here'"]

这似乎告诉 powershell 运行bash -c 'insert code here'。谁能想出正确的命令?


一些背景:

${file} 是一个变量,表示 VSCode 中的活动文件。这是所有predefined variables的列表。

wslpath 是 Windows 1803 中的新 bash 命令,它采用 Windows 路径并返回 wsl 路径,例如:

$ wslpath "c:\Users\Evan\Google Drive\Development\Programming Languages\file.c"
/mnt/c/Users/Evan/Google Drive/Development/Programming Languages/file.c

我的尝试:

我尝试编写脚本来执行此操作,但遇到了问题。调用myscript 的tasks.json 语法应该是这样的(注意我们将文件名传递给脚本,以便脚本可以将其传递给GCC)。

"args": ["-c", "'myscript.sh \"${file}\"'"]

或者按照这个answer

"command": "/path/to/script.sh"
"args": ["\"${file}\""]

myscript 可能看起来像这样:

#!/bin/bash
echo "File Path is here $1"
x=$(wslpath "$1")
gcc "$x" -o a.out
./a.out

VS Code Docs

【问题讨论】:

    标签: bash visual-studio-code windows-subsystem-for-linux


    【解决方案1】:

    不用脚本也可以:

     "command": "gcc '$(wslpath '${file}')' && ./a.out"
    

    ${file} 将在 bash 中解析命令之前被解析。

    【讨论】:

    • 这不起作用。它被我的路径名中的空格弄糊涂了。我收到以下错误:> Executing task: gcc $(wslpath 'c:\Users\Evan\Google Drive\Development\Programming Languages\C\sandbox.c') < gcc: error: /mnt/c/Users/Evan/Google: No such file or directory gcc: error: Drive/Development/Programming: No such file or directory gcc: error: Languages/C/sandbox.c: No such file or directory gcc: fatal error: no input files compilation terminated. The terminal process terminated with exit code: 1
    • 我想通了;你只需要一组额外的转义引号\" 来处理这种情况:"command": "gcc \"$(wslpath '${file}')\" && echo done"
    • 然后运行文件也很简单:"gcc \"$(wslpath '${file}')\" -o a.out &&./a.out
    • 为我工作,没有额外的报价。在 vs-code 1.23.1 和 windows 1803 上。添加了运行可执行命令 o answer。
    • 你的路径名中有空格吗?
    【解决方案2】:

    我想通了。首先,确保您的用户设置配置为使用bash

    "terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\bash.exe"
    

    注意"C:\\Windows\\System32\\bash.exe" 似乎也可以正常工作。


    接下来你的 tasks.json 应该包含这些行:

    "type": "shell",                                        //Uses the default shee (make sure this is set to bash)
    "command": "'./path to/your/script.sh'",                //Unix Style Path to the Script (remember, bash is your shell reading this).
    "args": ["\"${file}\""],                                //Pass the path to the source code as an arg 
    "useWSL": true,                                         //This was suggested somewhere. The script runs fine without it though.... Not sure what this does. 
    

    最后,这是脚本。 务必将行尾更改LF。否则该脚本不起作用。

    #!/bin/bash
    echo "Running Script"
    echo "Windows File Path $1"           #The Windows file path we passed in as an argument
    wslpath=$(wslpath "$1")               #Convert the Windows path to a Unix Style Path
    echo "wslpath is $wslpath"
    gcc "$wslpath"                        #Compile the file
    echo "done" 
    ./a.out                               #execute the compiled file
    

    然后打开任何 C 文件并按 Ctrl + Shift + B 并从构建任务菜单中选择脚本名称,会将 C 文件编译为当前目录中的 a.out。

    【讨论】:

      【解决方案3】:

      为避免更改默认 shell,让它在 Powershell 中工作,并避免长转义序列,最好使用单独的文件。

      启动器:

      {
                  "label": "command name",
                  "type": "shell",
                  "command": ".\\command_name.bat",
                  "problemMatcher": []
      }
      

      批处理文件:

      bash.exe -c "ssh -T bastion \"for proxy in bunch of different hosts ; do echo \\\$proxy ; ssh \\\$proxy -T -oBatchMode=yes -oStrictHostKeyChecking=no -oConnectTimeout=3 \\\"command --with --parameters\\\" ; done\""
      

      【讨论】:

        【解决方案4】:

        这里是另一个快速解决方案,如何使用 vscode 中task.json 文件中的bash 命令解决问题。您几乎可以使用它并插入您需要的命令。如果需要,您还可以链接和嵌套命令。

        {
            "version": "2.0.0",
            "tasks": [
                {
                    // link current file. Uses one or more object.o files
                    "label": "link_only_cpp_17",
                    "type": "shell",
                    "command": "bash", // call bash
                    "args": [
                        "-c", // read commands from string
                        "\"g++ -g -o a $(ls | grep -F .o | tr '\\n' ' ')\"" // link all files ending with .o
                    ],
                    "problemMatcher": []
                }
            ]
        }
        

        这是我的task.json 文件中的一个示例。它表明您可以使用bash 命令并将任意代码传递给它。为此,您必须使用-c 标志(从字符串中读取代码),然后将代码作为字符串传递(下一个参数)。在此示例中,g++ 链接所有包含 .o 的文件。这些文件是在$(ls | grep -F .o | tr '\\n' ' ') 的帮助下找到的。请注意,您必须使用$(),否则您的代码将不会被评估。

        据我所知,您不能在传递给 bash 的命令中使用 vscode 宏(例如 ${file}),因为这是一个字符串文字。这就是我使用a 而不是文件名的原因。如果你知道这样做的方法,请告诉我:)

        适用于 vscode 版本 1.38.1。

        【讨论】:

          【解决方案5】:

          一个简单的解决方法:在命令前添加wsl

          【讨论】:

            猜你喜欢
            • 2023-03-28
            • 1970-01-01
            • 2018-07-08
            • 2022-08-15
            • 2021-01-19
            • 1970-01-01
            • 2017-11-11
            • 1970-01-01
            • 2020-09-08
            相关资源
            最近更新 更多