【运维经】第42章——从 WSL 运行 Windows 工具

运维经–目录


从 WSL 运行 Windows 工具

WSL 可以使用 [binary name].exe 直接从 WSL 命令行调用 Windows 二进制文件。 例如, notepad.exe。 为使 Windows 可执行文件更易于运行,Windows 路径将包含在 Fall Creators Update 中的 Linux $PATH 内。
以这种方式运行的应用程序具有以下属性:

  1. 按 WSL 命令提示保留工作目录(大部分情况下是这样 – 下面所述的情况除外)。
  2. 拥有与 WSL 进程相同的权限。
  3. 以活动 Windows 用户的身份运行。
  4. 显示在 Windows 任务管理器中,就如同直接从 CMD 提示符执行的一样。

示例:

$ notepad.exe

在 WSL 中运行的 Windows 可执行文件的处理方式类似于本机 Linux 可执行文件 - 管道处理、重定向,甚至后台处理都可按预期方式工作。
使用管道的示例:

$ ipconfig.exe | grep IPv4 | cut -d: -f2
 192.168.10.42
 192.168.3.36

使用混合 Windows 和 WSL 命令的示例:

$ ls -la | findstr.exe foo.txt
$ cmd.exe /c dir
<- contents of C:\ ->

Windows 二进制文件必须包含文件扩展名,匹配文件大小写,并且可执行。 包含批处理脚本的不可执行文件。dir等 CMD 本机命令可与cmd.exe /C 命令一起运行。

示例:

$ cmd.exe /C dir
<- contents of C:\ ->

$ PING.EXE www.microsoft.com
Pinging e1863.dspb.akamaiedge.net [2600:1409:a:5a2::747] with 32 bytes of data:
Reply from 2600:1409:a:5a2::747: time=2ms

参数将按原样传递到 Windows 二进制文件。
例如,以下命令将在 C:\temp\foo.txt 中打开 notepad.exe:

$ notepad.exe "C:\temp\foo.txt"
$ notepad.exe C:\\temp\\foo.txt

不支持使用 WSL 中的 Windows 应用程序修改位于 VolFs 中的文件(不是在 /mnt/<x> 下的文件)。
默认情况下,WSL 会尝试将 Windows 二进制文件的工作目录保留为当前的 WSL 目录,但如果工作目录位于 VolFs 中,则会回退到实例创建目录。
例如,wsl.exe 最初是从 C:\temp 启动的,而当前 WSL 目录已更改为用户的主目录。 如果从用户的主目录调用notepad.exeWSL 将自动还原到 C:\temp(notepad.exe 工作目录):

C:\temp> wsl
/mnt/c/temp/$ cd ~
~$ notepad.exe foo.txt
~$ ls | grep foo.txt
~$ exit

exit
C:\temp>dir | findstr foo.txt
09/27/2016  02:15 PM                14 foo.txt
发布了82 篇原创文章 · 获赞 14 · 访问量 9147

猜你喜欢

转载自blog.csdn.net/xk_xx/article/details/104675117
今日推荐