Reputation: 9
i have this bash script that uses wget to grab a web-page title this works perfect if i run it as is i give it a url and it shows me the result in the terminal
problem is i want to be able to save it to a file but only if the result is a exact match to a specified word like INEEDTHISSTRING
so if it matches the string above add the url used to result.txt (already contains urls) and move on to the next line
if not then do not do anything and move onto the next line
here is my chktitle.sh file contents can any body help me
#!/bin/bash
string=$1"/search/"
wget --quiet -O - $string \
| sed -n -e 's!.*<title>\(.*\)</title>.*!\1!p'
Upvotes: 0
Views: 328
Reputation: 8402
#! /bin/bash
for i in $1; do
var=`wget --quiet -O - "$i"|sed -n -e 's!.*<title>\(.*\)</title>.*!\1!p'| grep -o '\<INEEDTHISSTRING\>'`
if [ -n "$var" ]; then
echo "$i" >> result.txt
else
continue
fi
done
Note: My interpretation of the question
You have several urls separated by a space as one big arugment enclosed by single quotes as follows:
chktitle.sh 'www.google.com www.yahoo.com wwww.hotmail.com'
For each url the codes will wget that url title.If there are any words in the title matches your string 'INEEDTHISSTRING',it will store the url and move on to the next url. If no match found for a url it will skip that url and move on.
Please advise me if I didn't get the full grasp of your question Also ensure you delete any result.txt file before running the codes since it will append the results to your existing result.txt
Upvotes: 1
Reputation: 185570
If I understand you well:
#!/bin/bash
url="$1/search/"
wget --quiet -O - "$url" |
grep -q '<title>INEEDTHISSTRING</title>' &&
echo "$url" >> result.txt
Upvotes: 1