LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help with shell programming (https://www.linuxquestions.org/questions/linux-newbie-8/help-with-shell-programming-708588/)

kilo_911 03-02-2009 09:13 AM

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.

angel115 03-02-2009 10:20 AM

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.

kilo_911 03-02-2009 10:34 AM

Quote:

Originally Posted by angel115 (Post 3462490)
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

angel115 03-02-2009 11:07 AM

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.


All times are GMT -5. The time now is 11:35 AM.