bash 如何识别链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20175313/
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
How does bash recognize a link?
提问by Roxana Ciobanu
In a bash script that captures a href link, how does bash know that TEXT is a link and not a typical string? Why at the end when $TEXT="www.google.com", TEXT is still a link? Can you do that with a file? For example having a 'Click me' that runs a scrip.
在捕获 href 链接的 bash 脚本中,bash 如何知道 TEXT 是链接而不是典型的字符串?为什么在 $TEXT="www.google.com" 的末尾,TEXT 仍然是一个链接?你能用文件做到这一点吗?例如,有一个运行脚本的“点击我”。
shopt -s nocasematch
TEXT='<a href="http://www.google.com/search/something/lulz/here2 i=!mfo1iu489fn1o2jlk21m4098mdoi">"test link"</a><br>'
TEXT=${TEXT##*href=\"}
TEXT=${TEXT%%\"*}
TEXT=${TEXT##*//}
TEXT=${TEXT%%/*}
echo $TEXT
采纳答案by Mateusz Kubuszok
I'd say that bash itself doesn't recognize links. But some terminal emulators does
我会说 bash 本身无法识别链接。但是一些终端模拟器确实
- in gnome terminal when you type
echo http://www.google.com
or 'echo www.google.com' terminal with allow you to click on link, butecho google.com
will not be recognized as one - it most likely depends onhttp://
prefix (clearly indicating that it's a link) orwww
prefix (often occurring at www adress), - E17's terminology act similarly it gnome teminal in this regard,
- probably most other terminal emulators will act in a similar manner, but that 2 I can confirm.
- 在 gnome 终端中,当您键入
echo http://www.google.com
或“echo www.google.com”终端时,允许您单击链接,但echo google.com
不会被识别为一个链接 - 它很可能取决于http://
前缀(清楚地表明它是一个链接)或www
前缀(通常发生在 www 地址), - E17 的术语在这方面与侏儒终端类似,
- 可能大多数其他终端模拟器都会以类似的方式运行,但我可以确认的是 2。
From how bash scripts are used I'd say that there is no implementation independent way to add clickable URLs into bash. But you can used described way to obtain similar behavior in terminal emulators. Alternatively, obtain from user some yes-no variable, and use it in conditional program call:
从 bash 脚本的使用方式来看,我想说没有独立于实现的方法可以将可点击的 URL 添加到 bash 中。但是您可以使用描述的方式在终端模拟器中获得类似的行为。或者,从用户那里获取一些 yes-no 变量,并在条件程序调用中使用它:
read openpage
if $openpage -eq "yes"
# open browser with your URL
fi
where how to open webpage is described e.g. here.
例如这里描述了如何打开网页。
As for the files, I'd stay with just if $runfile -eq "yes" then command; fi
. Basically bash wasn't made with any GUI interaction on mind - it's a terminal after all.
至于文件,我只保留if $runfile -eq "yes" then command; fi
. 基本上 bash 并没有考虑到任何 GUI 交互——毕竟它是一个终端。