bash 从 shell 脚本启动 Web 浏览器的干净方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3124556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:24:00  来源:igfitidea点击:

Clean way to launch the web browser from shell script?

bashshellcommand-linestandardsstandards-compliance

提问by nicoulaj

In a bash script, I need to launch the user web browser. There seems to be many ways of doing this:

在 bash 脚本中,我需要启动用户 Web 浏览器。似乎有很多方法可以做到这一点:

  • $BROWSER
  • xdg-open
  • gnome-openon GNOME
  • www-browser
  • x-www-browser
  • ...
  • $BROWSER
  • xdg-open
  • gnome-open在 GNOME 上
  • www-browser
  • x-www-browser
  • ...

Is there a more-standard-than-the-others way to do this that would work on most platforms, or should I just go with something like this:

是否有一种比其他方法更标准的方法可以在大多数平台上运行,或者我应该使用这样的方法:

#/usr/bin/env bash

if [ -n $BROWSER ]; then
  $BROWSER 'http://wwww.google.com'
elif which xdg-open > /dev/null; then
  xdg-open 'http://wwww.google.com'
elif which gnome-open > /dev/null; then
  gnome-open 'http://wwww.google.com'
# elif bla bla bla...
else
  echo "Could not detect the web browser to use."
fi

采纳答案by Philipp

xdg-openis standardized and should be available in most distributions.

xdg-open是标准化的,应该在大多数发行版中可用。

Otherwise:

除此以外:

  1. evalis evil, don't use it.
  2. Quote your variables.
  3. Use the correct test operators in the correct way.
  1. eval是邪恶的,不要使用它。
  2. 引用你的变量。
  3. 以正确的方式使用正确的测试运算符。

Here is an example:

下面是一个例子:

#!/bin/bash
if which xdg-open > /dev/null
then
  xdg-open URL
elif which gnome-open > /dev/null
then
  gnome-open URL
fi

Maybe this version is slightly better (still untested):

也许这个版本稍微好一点(仍未测试):

#!/bin/bash
URL=
[[ -x $BROWSER ]] && exec "$BROWSER" "$URL"
path=$(which xdg-open || which gnome-open) && exec "$path" "$URL"
echo "Can't find browser"

回答by jfs

python -mwebbrowser http://example.com

works on many platforms

适用于许多平台

回答by mbs400

OSX:

操作系统:

$ open -a /Applications/Safari.app http://www.google.com

or

或者

$ open -a /Applications/Firefox.app http://www.google.com

or simply...

或者干脆……

$ open some_url

回答by Joan Rieu

You could use the following:

您可以使用以下内容:

x-www-browser

It won't run the user's but rather the system's default X browser.

它不会运行用户的而是系统的默认 X 浏览器。

See: this thread.

请参阅:此线程。

回答by jamescampbell

Taking the other answers and making a version that works for all major OS's as well as checking to ensure that a URL is passed in as a run-time variable:

获取其他答案并制作适用于所有主要操作系统的版本,并检查以确保 URL 作为运行时变量传入:

#!/bin/bash
if [ -z  ]; then
  echo "Must run command with the url you want to visit."
  exit 1
else
  URL=
fi
[[ -x $BROWSER ]] && exec "$BROWSER" "$URL"
path=$(which xdg-open || which gnome-open) && exec "$path" "$URL"
if open -Ra "safari" ; then
  echo "VERIFIED: 'Safari' is installed, opening browser..."
  open -a safari "$URL"
else
  echo "Can't find any browser"
fi

回答by Raphi

This may not apply exactly to what you want to do, but there is a really easy way to create and launch a server using the http-servernpm package.

这可能并不完全适用于您想要做的事情,但是有一种使用http-servernpm 包创建和启动服务器的非常简单的方法。

Once installed (just npm install http-server -g) you can put

一旦安装(只是npm install http-server -g)你可以把

http-server -o

http-server -o

in your bash script and it will launch a server from the current directory and open a browser to that page.

在您的 bash 脚本中,它将从当前目录启动服务器并打开浏览器到该页面。