Register
It is currently Fri Apr 18, 2014 7:10 am

cool, but malfunctioning, bash functions; repairs?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Fri Feb 08, 2013 1:03 pm   

Joined: Fri Feb 08, 2013 12:49 pm
Posts: 3
Hi. I'm not any kind of bash afficianado: I can at least handle some of the basics, though. Recently I found some cool functions that did look pretty cool. The link to the site where they're described is http://www.novell.com/coolsolutions/tools/18639.html . Especially the translate, define, and weather entries look useful. Problem is, that entry is dated enough that the functions no longer work. Even more of a problem is that these functions are complex enough that I can't really puzzle out where they may be malfunctioning. I suppose it may be simply because of the way the URL's have changed in the intervening years. I assume members of this list who are a lot better versed in this than I am may be able to spot and fix the problems and make the functions work once again. Anyone willing to have a go at that?

Thanks,
James

PS Relevant functions included below for reference:
Code:
weather ()
{
declare -a WEATHERARRAY
WEATHERARRAY=( `lynx -dump "http://www.google.com/search?hl=en&lr=&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&q=weather+${1}&btnG=Search" | grep -A 5 -m 1 "Weather for"`)
echo ${WEATHERARRAY[@]}
}

Code:
translate ()
{
TRANSLATED=`lynx -dump "http://dictionary.reference.com/browse/${1}" | grep -i -m 1 -w "${2}:" | sed 's/^[ \t]*//;s/[ \t]*$//'`
if [[ ${#TRANSLATED} != 0 ]] ;then
   echo "\"${1}\" in ${TRANSLATED}"
   else
   echo "Sorry, I can not translate \"${1}\" to ${2}"
fi
}

Code:
# Define a word - USAGE: define dog
define ()
{
lynx -dump "http://www.google.com/search?hl=en&q=define%3A+${1}&btnG=Google+Search" | grep -m 3 -w "*"  | sed 's/;/ -/g' | cut -d- -f1 > /tmp/templookup.txt
         if [[ -s  /tmp/templookup.txt ]] ;then   
            until ! read response
               do
               echo "${response}"
               done < /tmp/templookup.txt
            else
               echo "Sorry $USER, I can't find the term \"${1} \""            
         fi   
rm -f /tmp/templookup.txt
}


Top
 Profile  
 PostPosted: Fri Feb 08, 2013 4:54 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 512
hi,

Quote:
I suppose it may be simply because of the way the URL's have changed in the intervening years.
probably.
at first sight, codes seem right.

what error(s) do they output?


Top
 Profile  
 PostPosted: Fri Feb 08, 2013 5:04 pm   

Joined: Fri Feb 08, 2013 12:49 pm
Posts: 3
Well, if I type define hope, I get "Angeles 147 Standard Street El Segundo, CA 90245. JOIN HOPE. Email: *" If I type define word I get 'Sorry user, I can't find the term "word ".' If I type translate house spanish, as the example says I can, I get 'Sorry, I can not translate "house" to spanish.'


Top
 Profile  
 PostPosted: Fri Feb 08, 2013 5:47 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 512
don't you have a browser to test the addresses?

if the addresses are wrong, then the tested variables are empty, and thus the functions print the intended output for that case.


Top
 Profile  
 PostPosted: Fri Feb 08, 2013 6:14 pm   

Joined: Fri Feb 08, 2013 12:49 pm
Posts: 3
Thanks for your response, Watael. Yes, I do, of course, have a browser. But isn't it probable that the URL is not the only problem? So far as I can understand, these functions somehow process what the browser (lynx) returns. And I have no idea how the processing is done and what output is expected to be returned for the processing to occur. So how far, really, does confirming that URL's are not valid, get me? Not very far, I think. Maybe I'm just asking about this in the wrong place. I have just begun to understand what a bash function is; diagnosing one that's anything other than extremely simplistic may lie somewhere far in the future for me, but I certainly cannot do so at my current level of knowledge. I don't intend to pester anyone about this so I'll simply give up on these if my understanding is deemed too deficient to interact here.


Top
 Profile  
 PostPosted: Fri Feb 08, 2013 8:48 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 512
Quote:
So far as I can understand, these functions somehow process what the browser (lynx) returns.
you're right.

lynx download informations from addresses, and grep try to match a pattern against that flow; just like it would do for this
Code:
echo "foo
bar
baz" | grep 'bar'

but, as sites have changed, grep can't match much, and the variable is empty, so the conditional statement returns the "is false" case.


Top
 Profile  
 PostPosted: Wed Sep 04, 2013 2:43 pm   

Joined: Wed Sep 04, 2013 2:32 pm
Posts: 4
Try mine...

Dependencies
Code:

checkElinks() {
if ! which elinks > /dev/null; then
echo "Installing..."
sudo pacman -S elinks
fi  ; }

checkIspell() {
if ! which ispell > /dev/null; then
echo "Installing..."
sudo pacman -S ispell
fi  ; }

ping9() {
clear &&
host1=google.com
host2=gnu.org
curr_date=`date +"%Y%m%d%H%M"`
echo -n "DATE:${curr_date};"
((ping -w4 -c2 $host1 || ping -w4 -c2 $host2) > /dev/null 2>&1) && echo "OK!" || (echo "No internet connection!" && exit 1) ; }

spell() { echo $@ | ispell | grep -v "@(#)" | head -1 ; }


Code:
translate() { checkIspell && checkElinks && ping9 && spell ${1} && elinks -dump "http://www.collinsdictionary.com/dictionary/english-thesaurus/${1%%/}" |grep -A 300 "in Other Languages"| grep -v "in Other Languages"|grep -B 300 "Sponsored Links"| grep -v "Sponsored Links" ; } # Translate a single word only.


Code:
define() { checkIspell && checkElinks && ping9 && spell ${1} && elinks -dump "http://gcide.gnu.org.ua/?q=${1%%/}&define=1" | grep -A 300 "Found" | grep -B 300 "Powered by" | grep -v "GNU Dico Team."| grep --color -E '^|$1|noun|pronoun|adjective|acronym|homophone|synonym|verb|adverb|informal|Prov.|Noun||Pronoun|Adjective|Acronym|Homophone|Synonym|Verb|Adverb|Syn.' && echo -e "${BCyan}Powered by GNU Dico." && echo -e "${BGreen}This text is available under the GNU General Public License." ; }


Code:
weather ()
{
declare -a WEATHERARRAY
WEATHERARRAY=( `elinks -dump "http://www.google.com/search?hl=en&lr=&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&q=weather+myHometownOrPostcodeHere&btnG=Search" | grep -A 5 -m 1 "Weather for" | grep -v "Add to "`)
echo ${WEATHERARRAY[@]}
}


If you are on arch, Elinks and Ispell will automatically installed,
otherwise install manually "sudo apt-get install elinks ispell" etc.
I also have scripts for pronunciations and a thesaurus.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP