Windows 10上的WSL允许通过bash.exe执行Linux命令和命令行工具。非常有用的是,可以通过将Windows工具/命令作为参数传递给bash.exe来从Windows命令行(cmd.exe
)调用Linux工具/命令,如下所示:
bash.exe -c <linux command>
这非常有用,因为它应该允许基于Windows的脚本无缝地组合Windows和Linux工具。
不幸的是,我无法从R脚本调用Linux命令(见下文)。
0)系统
Win10 x64 +周年更新+ WSL已安装
1)比较情况,其中调用Linux命令起作用
以下所有对我有用的工作;此处显示的只是对
ls
的示例调用。Windows命令行中的
bash -c "ls /mnt/a"
bash -c "ls /mnt/a > /mnt/a/test.txt"
.bat
文件中也可以使用。 ShellExecute
对32位和64位Delphi XE2进行了测试:例如,这些工作(32位和64位):
ShellExecute (0, PChar('open'), PChar('cmd.exe'), PChar('/c c:\windows\system32\bash.exe -c "ls /mnt/a > /mnt/a/test.txt"'), nil, SW_SHOWNORMAL);
或(32位代码):
ShellExecute (0, PChar('open'), PChar('c:\windows\sysnative\bash.exe'), PChar('-c "ls /mnt/a > /mnt/a/test.txt"'), nil, SW_SHOWNORMAL);
或(64位代码):
ShellExecute (0, PChar('open'), PChar('c:\windows\system32\bash.exe'), PChar('-c "ls /mnt/a > /mnt/a/test.txt"'), nil, SW_SHOWNORMAL);
所有这些似乎都有效(并且
ShellExecute
返回42)。 2)无法从R 调用Linux命令
使用R 3.3.1 x64
以下所有内容(以及我尝试过的一些类似操作)均失败,状态为65535:
shell('c:/windows/system32/bash.exe -c "ls /mnt/a"', shell="cmd.exe", flag = "/c")
shell("ls", shell="c:/windows/system32/bash.exe", flag = "-c")
system('cmd /c c:/windows/system32/bash.exe -c "ls /mnt/a > /mnt/a/test.txt"')
system('bash -c "ls /mnt/a"')
system('c:/windows/system32/bash.exe -c "ls /mnt/a > /mnt/a/test.txt"')
3)问题
鉴于1)下的示例有效,我发现2)非常令人困惑。我在这里错过任何明显的东西吗?
对于一个简单的示例,在WSL下可以通过
bash.exe
运行Linux命令,我将不胜感激。 最佳答案
您失败的示例现在应该在Windows 10 Insider内部版本> = 14951 which introduced many "interop" improvements and new capabilities中正确运行:
> system('bash -c "ls /"')
产生:
bin cache dev home lib media opt root sbin srv tmp var
boot data etc init lib64 mnt proc run snap sys usr