清洁的方法来启动从shell脚本的Web浏览器? [英] Clean way to launch the web browser from shell script?
本文介绍了清洁的方法来启动从shell脚本的Web浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在bash脚本,我需要启动用户的Web浏览器。似乎有这样做的许多方面:
-
$ BROWSER
-
XDG-打开
-
的gnome-打开
在GNOME -
WWW浏览器
-
的X WWW浏览器
- ...
有没有更标准超了,其他的方式来做到这一点,将在大多数平台上工作,或者我应该只是像这样去:
#在/ usr /斌/ bash的ENV如果[-n $ BROWSER]。然后
$ BROWSERhttp://wwww.google.com
ELIF这XDG开>的/ dev / null的;然后
XDG-开放http://wwww.google.com
ELIF GNOME其中开放式>的/ dev / null的;然后
GNOME开http://wwww.google.com
#ELIF唧唧歪歪...
其他
回声无法检测到Web浏览器来使用。
科幻
解决方案
XDG-打开
是标准化的,应在大多数发行版中提供。
否则:
-
评估
是邪恶的,不使用它。 - 引用您的变量。
- 使用正确的测试运营商在正确的道路。
下面是一个例子:
#!/斌/庆典
如果这XDG开>的/ dev / null的
然后
XDG-打开URL
ELIF GNOME其中开放式>的/ dev / null的
然后
GNOME-打开URL
科幻
也许这个版本是稍微好一点(仍未经测试):
#!/斌/庆典
URL = $ 1
[-x $ BROWSER]&放大器;&安培; EXEC$浏览器$网址
PATH = $(其中XDG-开放|| GNOME其中开放式)及和放大器; EXEC$ PATH,$ URL
回声无法找到浏览器
In a bash script, I need to launch the user web browser. There seems to be many ways of doing this:
$BROWSER
xdg-open
gnome-open
on GNOMEwww-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
解决方案
xdg-open
is standardized and should be available in most distributions.
Otherwise:
eval
is evil, don't use it.- Quote your variables.
- Use the correct test operators in the correct way.
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=$1
[[ -x $BROWSER ]] && exec "$BROWSER" "$URL"
path=$(which xdg-open || which gnome-open) && exec "$path" "$URL"
echo "Can't find browser"
这篇关于清洁的方法来启动从shell脚本的Web浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文