Shell脚本打开URL [英] Shell script to open a URL

查看:192
本文介绍了Shell脚本打开URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写一个简单的shell脚本(例如script.sh),以便在执行时可以将URL作为参数传递?

How do I write a simple shell script (say script.sh), so that I can pass a URL as an argument while executing?

我希望浏览器从在该URL上打开的页面开始.我想在脚本中编写命令以打开浏览器并打开参数中给出的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.

推荐答案

方法1

假设您的浏览器是Firefox,脚本 urlopener

Suppose your browser is Firefox and your script urlopener is

#!/bin/bash
firefox "$1"

运行方式

./urlopener "https://google.com"


边注

用浏览器的可执行文件名替换 firefox .

Replace firefox with your browser's executable file name.

方法2

正如评论中提到的 [@ sato-katsura] 一样,您可以在* nixes中使用名为 xdg-open .例如,

As [ @sato-katsura ] mentioned in the comment, in *nixes you can use an application called xdg-open. For example,

xdg-open https://google.com

xdg-open 的手册说

xdg-open-在用户首选的应用程序中打开文件或URLxdg-open在用户的首选应用程序中打开文件或URL.如果一个提供了URL,该URL将在用户的首选网站中打开浏览器.
如果提供了文件,则该文件将在该类型文件的首选应用程序.xdg-open支持文件,ftp,http和https网址.

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.

[此] 答案指出,您可以使用以下方式更改您喜欢的浏览器:

As [ this ] answer points out you could change your preferred browser using say:

xdg-settings set default-web-browser firefox.desktop

xdg-settings set default-web-browser chromium-browser.desktop

这篇关于Shell脚本打开URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆