jan*_*pio 38 browser bash ruby windows-subsystem-for-linux
我有一个包含这一行的 ruby 脚本:
system("open '#{html_path}'")
Run Code Online (Sandbox Code Playgroud)
html_path是本地.html文件的路径。因此,脚本会打开一个浏览器,我通常可以在其中检查和查看此文件。
与 MacOS 环境相比,这在bashWSL 中不起作用。运行脚本没有任何反应,当我open直接在控制台中执行调用时,我得到了这个:
sujan@LenovoX1:/mnt/c/Users/Jan/Documents/foo$ open Preview.html
Couldn't get a file descriptor referring to the console
Run Code Online (Sandbox Code Playgroud)
我已经调查open过在 Ubuntu中做了一些不同的事情,我应该使用see或xdg-open打开一个文件。
不幸的是,这些在 WSL 中也不起作用:
sujan@LenovoX1:/mnt/c/Users/Jan/Documents/foo$ see ./Preview.html
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/%{ <-- HERE (.*?)}/ at /us
r/bin/see line 528.
Couldn't find a suitable web browser!
Set the BROWSER environment variable to your desired browser.
Warning: program returned non-zero exit code #1
Run Code Online (Sandbox Code Playgroud)
和
sujan@LenovoX1:/mnt/c/Users/Jan/Documents/foo$ xdg-open ./Preview.html
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/%{ <-- HERE (.*?)}/ at /us
r/bin/run-mailcap line 528.
Couldn't find a suitable web browser!
Set the BROWSER environment variable to your desired browser.
Warning: program returned non-zero exit code #1
/usr/bin/xdg-open: 771: /usr/bin/xdg-open: www-browser: not found
/usr/bin/xdg-open: 771: /usr/bin/xdg-open: links2: not found
/usr/bin/xdg-open: 771: /usr/bin/xdg-open: elinks: not found
/usr/bin/xdg-open: 771: /usr/bin/xdg-open: links: not found
/usr/bin/xdg-open: 771: /usr/bin/xdg-open: lynx: not found
/usr/bin/xdg-open: 771: /usr/bin/xdg-open: w3m: not found
xdg-open: no method available for opening './Preview.html'
Run Code Online (Sandbox Code Playgroud)
仔细想想,这是有道理的:WSL 中没有可用的浏览器。
我可以以某种方式设置这个BROWSER变量以便see在bashWSL内工作吗?
额外问题:如果是,如何在不更改代码的情况下使 ruby 脚本工作?这是一个外部依赖:/
小智 26
您可以设置BROWSER变量。有一篇很长的文章解释了如何设置环境变量。
如果要为当前终端会话设置变量,可以使用:
export BROWSER='/mnt/c/Program Files/Firefox/firefox.exe'
(假设您想使用 Firefox 并将其安装在C:\Program Files\Firefox\firefox.exe)
如果您希望这是持久的,您可以将上面的行添加到文件中~/.bashrc。
小智 24
安装wslu并使用 'BROWSER=wslview'
小智 14
出于某种原因,WSL 可以看到 windows 文件资源管理器。如果你这样做:
explorer.exe Preview.html
Run Code Online (Sandbox Code Playgroud)
在控制台中,它应该在默认的 Windows Web 浏览器中打开。
小智 9
结合上面的两个答案,最简单的解决方案是将 BROWSER 设置为 windows 文件资源管理器,以便使用默认的 Windows Web 浏览器
export BROWSER='/mnt/c/Windows/explorer.exe'
Run Code Online (Sandbox Code Playgroud)
我sensible-browser http://localhost:8001/在带有 WSL2 的 Ubuntu 20.04 上使用
过
这是open来自 macOS 的 WSL的“穷人” :
alias open="powershell.exe /c start"
将其添加到您的~/.profile或~/.bashrc适当的,或在您的 WSL shell 中运行它,然后以下事情将起作用:
open . # opens current folder in Explorer as e.g. \\wsl$\Ubuntu\home\john\myapp
open foobar.txt # opens in notepad.exe
open README.md # opens in VSCode in Windows, for example
open http://example.com # opens in your default web browser in Windows
open Preview.html # opens in your default web browser in Windows, as file://wsl%24/Ubuntu/home/john/Preview.html
Run Code Online (Sandbox Code Playgroud)
缺点:这似乎只适用于相对路径,而不适用于绝对路径
如果有人制作了一个 shell 脚本来模拟macOS 版本,例如-a应用程序、-e编辑器、-R在 explorer.exe 中显示等,并使其使用绝对路径,那就太好了
基于先前的答案
export BROWSER=/some/path/to/chrome.exe可能是您正在寻找的解决方案,但是 chrome 的默认路径是/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe(带有空格和括号的路径,用于外壳转义)。如果应用程序使用带引号的变量版本"${BROWSER}"将成功打开 chrome ,这将正常工作。但是,如果您使用变量 unquoted $BROWSER,bash 会将其视为单个变量。这会破坏某些工具,例如xdg-open.
解决这个问题很简单,您可以将其符号链接到没有空格的路径。
ln -s "/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe" ~/.local/bin/chrome
export BROWSER=~/.local/bin/chrome
Run Code Online (Sandbox Code Playgroud)
或者,您可以update-alternatives像Brett指出的那样使用。最后,这也是一个符号链接,但在何时/是否需要更新链接指向的位置时具有更高的可见性。
update-alternatives --install "bin/host_chrome" "chrome" "/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe" 1
export BROWSER=host_chrome
Run Code Online (Sandbox Code Playgroud)