Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > macOS > Help with simple shell script

Help with simple shell script
Thread Tools
genevish
Mac Enthusiast
Join Date: Jan 1999
Location: Marietta, GA, USA
Status: Offline
Reply With Quote
Oct 4, 2005, 10:40 PM
 
Code:
#!/bin/bash if [curl http://www.appleturns.com/ > grep -q "mass backsies"] then echo "Jack is still away" else echo "He's back!" fi
I'm close I think. How do I search this page for the specified text, then do something if it's there?

Thanks,

-Scott
Scott Genevish
scott AT genevish DOT org
     
Gavin
Mac Elite
Join Date: Oct 2000
Location: Seattle
Status: Offline
Reply With Quote
Oct 5, 2005, 06:52 AM
 
I'd dump it to file, grep the file, then remove the file.
This uses grep to count the matches found and drops that number into a var as an int.
then just test the int.

Code:
#!/bin/bash curl http://www.appleturns.com/ > temp.html c=`grep -c "mass backsies" temp.html` if [ "$c" -ge "1" ] then echo "He's back!" else echo "Jack is still away" fi rm temp.html
You can take the dude out of So Cal, but you can't take the dude outta the dude, dude!
     
Dork.
Professional Poster
Join Date: Sep 2005
Location: Rochester, NY
Status: Offline
Reply With Quote
Oct 5, 2005, 07:31 AM
 
Originally Posted by genevish
Code:
if [curl http://www.appleturns.com/ > grep -q "mass backsies"]
I think you want a pipe there , not a redirect. That will send the output of the "curl" command directly to "grep" via STDIO without creating an intermediate file. Or something like that.

Try

Code:
if [curl http://www.appleturns.com/ | grep -q "mass backsies"]
[/QUOTE]
( Last edited by Dork.; Oct 5, 2005 at 08:40 AM. )
     
genevish  (op)
Mac Enthusiast
Join Date: Jan 1999
Location: Marietta, GA, USA
Status: Offline
Reply With Quote
Oct 5, 2005, 10:12 PM
 
OK, here's my final solution. I installed the command line tool growlnotify that can pass a message to Growl. This script runs every hour:

Code:
#!/bin/bash curl -s http://www.appleturns.com/ > temp.html c=`grep -c "mass backsies" temp.html` if [ $c == 0 ] then /usr/local/bin/growlnotify -s "Jack Alert" -m 'Jack is Back!' fi rm temp.html
Thanks for the help!
Scott Genevish
scott AT genevish DOT org
     
genevish  (op)
Mac Enthusiast
Join Date: Jan 1999
Location: Marietta, GA, USA
Status: Offline
Reply With Quote
Oct 11, 2005, 07:28 PM
 
OK, I was so pleased with how this worked, I'd like to modify it to detect whenever this website is updated. I see the pseudo-code as follows:
  1. Check if website has changed using Grep
  2. If changed, display alert and then re-write the text searched for in Grep.

I think I can muddle through re-writing the Grep expression, but I'm not sure how to dynamicaly re-write the script to display the new search string. Should I use something like cat $theScript > file.sh? Is there a better way?

Thanks,

-Scott
Scott Genevish
scott AT genevish DOT org
     
   
Thread Tools
 
Forum Links
Forum 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 On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 12:19 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,