From: Alexander Steinböck Date: Wed, 18 Feb 2009 15:21:11 +0000 (+0100) Subject: zshrc::zurl(): Testing existence of given URL by pinging it first. X-Git-Tag: v0.3.64~6 X-Git-Url: https://git.grml.org/?a=commitdiff_plain;ds=sidebyside;h=8c13f49a31fb01dafa85309149a2894874b52d76;p=grml-etc-core.git zshrc::zurl(): Testing existence of given URL by pinging it first. --- diff --git a/etc/zsh/zshrc b/etc/zsh/zshrc index fde07a3..61e86e5 100644 --- a/etc/zsh/zshrc +++ b/etc/zsh/zshrc @@ -3929,23 +3929,32 @@ function zurl() { local PN url tiny grabber search result preview PN=${0} url=${1} -# Prepend 'http://' to given URL where necessary for later output. - [[ ${url} != http(s|)://* ]] && url='http://'${url} - tiny='http://tinyurl.com/create.php?url=' - if check_com -c wget ; then - grabber='wget -O- -o/dev/null' +# Check existence of given URL with the help of ping(1). +# N.B. ping(1) only works without an eventual given protocol. + ping -c 1 ${${url#(ftp|http)://}%%/*} >& /dev/null || \ + read -q "?Given host ${${url#http://*/}%/*} is not reachable by pinging. Proceed anyway? [y|n] " + + if (( $? == 0 )) ; then +# Prepend 'http://' to given URL where necessary for later output. + [[ ${url} != http(s|)://* ]] && url='http://'${url} + tiny='http://tinyurl.com/create.php?url=' + if check_com -c wget ; then + grabber='wget -O- -o/dev/null' + else + print "wget is not available, but mandatory for ${PN}. Aborting." + fi +# Looking for i.e.`copy('http://tinyurl.com/7efkze')' in TinyURL's HTML code. + search='copy\(?http://tinyurl.com/[[:alnum:]]##*' + result=${(M)${${${(f)"$(${=grabber} ${tiny}${url})"}[(fr)${search}*]}//[()\';]/}%%http:*} +# TinyURL provides the rather new feature preview for more confidence. + preview='http://preview.'${result#http://} + + printf '%s\n\n' "${PN} - Shrinking long URLs via webservice TinyURL ." + printf '%s\t%s\n\n' 'Given URL:' ${url} + printf '%s\t%s\n\t\t%s\n' 'TinyURL:' ${result} ${preview} else - print "wget is not available, but mandatory for ${PN}. Aborting." + return 1 fi -# Looking for i.e.`copy('http://tinyurl.com/7efkze')' in TinyURL's HTML code. - search='copy\(?http://tinyurl.com/[[:alnum:]]##*' - result=${(M)${${${(f)"$(${=grabber} ${tiny}${url})"}[(fr)${search}*]}//[()\';]/}%%http:*} -# TinyURL provides the rather new feature preview for more confidence. - preview='http://preview.'${result#http://} - - printf '%s\n\n' "${PN} - Shrinking long URLs via webservice TinyURL ." - printf '%s\t%s\n\n' 'Given URL:' ${url} - printf '%s\t%s\n\t\t%s\n' 'TinyURL:' ${result} ${preview} } #f2# Print a specific line of file(s).