LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-02-2009, 09:13 AM   #1
kilo_911
LQ Newbie
 
Registered: Mar 2009
Posts: 2

Rep: Reputation: 0
Help with shell programming


Hello all,

I have a shell script where User types in a searchphrase ./test -s "Classical Music".....the script then passes the string to google using wget and stores the received file. After this it runs through the sed program several times to remove any unwanted materials from the file and to prepare it in correct format.....and eventually output the contents of the file into the screen in either plaintext or htmltable.

here is my script:
Code:
wget  -t1  -E -e robots=off - -awGet.log -T 200 -H -Priserless -O mylist1.html -U "Mozilla" "http://www.google.co.uk/search?q=classical+music&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-GB:unofficial&client=firefox-a"
# put a newline at the start of a link
cat mylist1.html |sed -e 's#<a href#\n<a href#g' >mylist2.html
cat mylist2.html |sed -e 's#</a>#</a>\n#g' >mylist3.html
cat mylist3.html |sed -n -e '/^<a href="http:/p' >mylist4.html
cat mylist4.html |sed -n -e '/\.google/!p' >mylist5.html
cat mylist5.html |sed -n -e '/[0-9]*[0-9]*[0-9]*\.[0-9]*[0-9]*[0-9]*\.[0-9]*[0-9]*[0-9]*\.[0-9]*[0-9]*[0-9]*/!p' >mylist6.html
How could I achieve this......

Thanks in advance.
 
Old 03-02-2009, 10:20 AM   #2
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 542

Rep: Reputation: 79
Hi kilo_911,


I don't really get your question.....

if your question is how to get the argument from your command line it's simple, use ${1} for the first argument, ${2} for the second and so one. the ${0} will retrieve the command name.

Note: I've remove the "-s" as I didn't know what do you wanted to do with it. (the simple the better )

Ex:
./test "Classical Music"
Code:
#!/bin/bash

#Check if the user type something to search
if [ "${1}" == "" ];
then
echo "please type a text to search:"
echo "Ex: test \"My Search\""
exit 1
fi
search=$( echo ${1} | sed -e 's/\ /\+/g' ) #We replace the space by the signe "+"

echo = "searching for: ${search}" #here I display the variable to make sure I've change it correctly.
wget  -t1  -E -e robots=off - -awGet.log -T 200 -H -Priserless -O mylist1.html -U "Mozilla" "http://www.google.co.uk/search?q=${search}&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-GB:unofficial&client=firefox-a"
# put a newline at the start of a link
cat mylist1.html |sed -e 's#<a href#\n<a href#g' >mylist2.html
cat mylist2.html |sed -e 's#</a>#</a>\n#g' >mylist3.html
cat mylist3.html |sed -n -e '/^<a href="http:/p' >mylist4.html
cat mylist4.html |sed -n -e '/\.google/!p' >mylist5.html
cat mylist5.html |sed -n -e '/[0-9]*[0-9]*[0-9]*\.[0-9]*[0-9]*[0-9]*\.[0-9]*[0-9]*[0-9]*\.[0-9]*[0-9]*[0-9]*/!p' >mylist6.html
Please let me know if that's what you are looking for.
Angel.

Last edited by angel115; 03-02-2009 at 10:35 AM. Reason: add a check to see if the user enter something to search
 
Old 03-02-2009, 10:34 AM   #3
kilo_911
LQ Newbie
 
Registered: Mar 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by angel115 View Post
Hi kilo_911,


I don't really get your question.....

if your question is how to get the argumnet from your command line it's simple: user ${1}
for the first argument


Ex:
./test "Classical Music"
Code:
#!/bin/bash

search=$( echo ${1} | sed -e 's/\ /\+/g' ) #We replace the space by the signe "+"

echo = "searching for: ${search}" #here I display the variable to make sure I've change it correctly.
wget  -t1  -E -e robots=off - -awGet.log -T 200 -H -Priserless -O mylist1.html -U "Mozilla" "http://www.google.co.uk/search?q=${search}&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-GB:unofficial&client=firefox-a"
# put a newline at the start of a link
cat mylist1.html |sed -e 's#<a href#\n<a href#g' >mylist2.html
cat mylist2.html |sed -e 's#</a>#</a>\n#g' >mylist3.html
cat mylist3.html |sed -n -e '/^<a href="http:/p' >mylist4.html
cat mylist4.html |sed -n -e '/\.google/!p' >mylist5.html
cat mylist5.html |sed -n -e '/[0-9]*[0-9]*[0-9]*\.[0-9]*[0-9]*[0-9]*\.[0-9]*[0-9]*[0-9]*\.[0-9]*[0-9]*[0-9]*/!p' >mylist6.html
This should do,
Angel.
Thanks Angel for your help

You are close enough as to what I am looking for......

What I want is say is the user types in ./test -s "Classical Music",

which passes this string to google using the 'wget' in my script to lookup google and then store all the information into the files 'mylist.html' which I have in myscript and run through 'sed' program by removing the superfluous materianls, finally displaying it on the terminal where the user passes the argument in either plaintext or html!

Thanks

Last edited by kilo_911; 03-02-2009 at 10:35 AM.
 
Old 03-02-2009, 11:07 AM   #4
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 542

Rep: Reputation: 79
Hi kilo_911,

Cool, happy to hear that; just one thing if I may comment.

you should drop the "-s" as it doesn't add any value to your script.


After several years of scripting I learn something:
#####
The simpler the better.
#####
So keep it simple and you will keep you self away from troubles.


Angel.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Programming sharadgana Programming 5 12-14-2004 07:42 AM
shell programming athenerx Programming 1 10-28-2001 04:33 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration