bash 用于打开 URL 的 Shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38147620/
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
Shell script to open a URL
提问by Rishav Sharan
How do I write a simple shell script (say script.sh), so that I can pass a URL as an argument while executing?
如何编写一个简单的 shell 脚本(比如 script.sh),以便在执行时可以将 URL 作为参数传递?
I want a browser to start with the page opened on that URL. I want to write the command in the script to open a browser and open the URL given in argument.
我希望浏览器从在该 URL 上打开的页面开始。我想在脚本中编写命令以打开浏览器并打开参数中给出的 URL。
采纳答案by sjsam
Method 1
方法一
Suppose your browser is Firefox and your script urlopener
is
假设你的浏览器是 Firefox 而你的脚本urlopener
是
#!/bin/bash
firefox ""
Run it like
运行它像
./urlopener "https://google.com"
Sidenote
边注
Replace firefox
with your browser's executable file name.
替换firefox
为浏览器的可执行文件名。
Method 2
方法二
As [ @sato-katsura ]mentioned in the comment, in *nixes you can use an application called xdg-open
. For example,
正如评论中提到的[@sato-katsura],在 *nixes 中,您可以使用名为xdg-open
. 例如,
xdg-open https://google.com
The manual for xdg-open
says
手册上xdg-open
说
xdg-open - opens a file or URL in the user's preferred application xdg-open opens a file or URL in the user's preferred application. If a URL is provided the URL will be opened in the user's preferred web browser.
If a file is provided the file will be opened in the preferred application for files of that type. xdg-open supports file, ftp, http and https URLs.
xdg-open - 在用户首选应用程序中打开文件或 URL xdg-open 在用户首选应用程序中打开文件或 URL。如果提供了 URL,则该 URL 将在用户首选的 Web 浏览器中打开。
如果提供了文件,则该文件将在该类型文件的首选应用程序中打开。xdg-open 支持文件、ftp、http 和 https URL。
As [ this ]answer points out you could change your preferred browser using say:
正如[这个]答案指出的那样,您可以使用以下方法更改首选浏览器:
xdg-settings set default-web-browser firefox.desktop
or
或者
xdg-settings set default-web-browser chromium-browser.desktop
回答by Eugene Yarmash
You don't need to write a script for that. There're some tools that you can use depending on your OS:
您无需为此编写脚本。根据您的操作系统,您可以使用一些工具:
Linux
Linux
xdg-open
is available in most Linux distributions. It opens a file or URL in the user's preferred browser (configurable with xdg-settings
).
xdg-open
在大多数 Linux 发行版中都可用。它会在用户首选的浏览器中打开一个文件或 URL(可通过 配置xdg-settings
)。
xdg-open https://stackoverflow.com
macOS
苹果系统
open
opens files and URLs in the default or specified application.
open
在默认或指定的应用程序中打开文件和 URL。
open https://stackoverflow.com
open -a Firefox https://stackoverflow.com
Windows
视窗
You can use the start
command at the command prompt to open an URL in the default (or specified) browser:
您可以start
在命令提示符下使用该命令在默认(或指定)浏览器中打开 URL:
start https://stackoverflow.com
start firefox https://stackoverflow.com
Cross-platform
跨平台
The builtin webbrowser
Python module works on many platforms.
内置的webbrowser
Python 模块适用于许多平台。
python -m webbrowser https://stackoverflow.com
python -m webbrowser https://stackoverflow.com
回答by Sangram Nandkhile
For Windows,
对于Windows,
You can just write start filename_or_URL
你可以写 start filename_or_URL
start https://www.google.com
It will open the URL in a default browser. If you want to specify the browser you can write:
它将在默认浏览器中打开 URL。如果要指定浏览器,可以编写:
start chrome https://www.google.com
start firefox https://www.google.com
start iexplore https://www.google.com
Note:The browser name above can be obtained from the exe
file found in program files (sample: C:\Program Files\Internet Explorer\iexplore.exe
) if you wish to open multiple URLs.
注意:如果您希望打开多个 URL ,可以从exe
程序文件(示例:)中找到的文件中获取上述浏览器名称C:\Program Files\Internet Explorer\iexplore.exe
。
start chrome "www.google.com" "www.bing.com"
It was tested with .sh (shellscript file) and .bat files.
它使用 .sh(shellscript 文件)和 .bat 文件进行了测试。
回答by Roc Boronat
In MacOS, just open
works. So, open "$1"
will open the passed URL in Chrome, if Chrome is the default browser.
在 MacOS 中,open
正常工作。因此,open "$1"
如果 Chrome 是默认浏览器,将在 Chrome 中打开传递的 URL。
回答by trve.fa7ad
If you want a cross-OS solution and are comfortable using Python (3):
如果你想要一个跨操作系统的解决方案并且习惯使用 Python (3):
Try this:
尝试这个:
import webbrowser
webbrowser.open('https://yoururl.com')
Or in a terminal/cmd:
或者在终端/cmd中:
python -m webbrowser -t "https://yoururl.com"
回答by Jithin Antony
start "" "browser_location" "address"
For example:
例如:
start "" "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "https://google.com"