VS 代码:运行 任务 WSL/Bash
VS Code: Running Tasks with WSL/Bash
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 路径和 returns 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}\"'"]
或按照此
"command": "/path/to/script.sh"
"args": ["\"${file}\""]
myscript
可能看起来像这样:
#!/bin/bash
echo "File Path is here "
x=$(wslpath "")
gcc "$x" -o a.out
./a.out
我明白了。首先,确保您的用户设置配置为使用 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 " #The Windows file path we passed in as an argument
wslpath=$(wslpath "") #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。
不用脚本也可以:
"command": "gcc '$(wslpath '${file}')' && ./a.out"
${file}
将在 bash.
中解析的命令之前被解析
为避免更改默认设置 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\""
这是另一个快速解决方案,如何使用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。
一个简单的解决方法:在命令前添加 wsl
。
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 路径和 returns 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}\"'"]
或按照此
"command": "/path/to/script.sh"
"args": ["\"${file}\""]
myscript
可能看起来像这样:
#!/bin/bash
echo "File Path is here "
x=$(wslpath "")
gcc "$x" -o a.out
./a.out
我明白了。首先,确保您的用户设置配置为使用 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 " #The Windows file path we passed in as an argument
wslpath=$(wslpath "") #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。
不用脚本也可以:
"command": "gcc '$(wslpath '${file}')' && ./a.out"
${file}
将在 bash.
为避免更改默认设置 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\""
这是另一个快速解决方案,如何使用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。
一个简单的解决方法:在命令前添加 wsl
。