Register
It is currently Thu Jul 31, 2014 11:45 pm

Executing Commands From a File


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Mon May 26, 2014 11:51 pm   

Joined: Mon May 26, 2014 8:26 pm
Posts: 2
I am trying to write my own dynamic DNS client to check my router's external IP address and upload it somewhere where I can find it. Here's the simplified script scrapeip:
Code:
#!/bin/bash
while read -r line
do
   echo Trying to scrape IP from: $line
   $line > scrapedip.txt
   if [ $? -eq 0 ]; then
      echo success   # ...and do something useful
      break
   fi
   echo fail
done < ipscrapers.txt

The script reads a file ipscrapers.txt, line by line, and tries to execute each line. If one succeeds it breaks the loop. For now I only have one line in ipscrapers.txt:
Code:
wget -qO- http://checkip.dyndns.org | sed -e 's/.*Current IP Address: //' -e 's/<.*$//'

When I run this script I get:
Quote:
some@one:~/scripts$ ./scrapeip
Trying to scrape IP from: wget -qO- http://checkip.dyndns.org | sed -e 's/.*Current IP Address: //' -e 's/<.*$//'
wget: Invalid --execute command ‘'s/.*Current’
fail
some@one:~/scripts$

When I run the wget line at the command line it works fine, also if I have it directly in the script it works. It does not work when I read the command line from a file. How can I fix this?
The point of all this is to eventually have a longer list of scrapees, and then rotate the list after every use round-robin style, spreading the load overs many servers.


Top
 Profile  
 PostPosted: Wed May 28, 2014 10:58 pm   

Joined: Mon May 26, 2014 8:26 pm
Posts: 2
I found the solution: 'eval' is the trick. I modified the script like so, and now it works (line 5):
Code:
#!/bin/bash
while read -r line
do
   echo Trying to scrape IP from: $line
   eval $line > scrapedip.txt
   if [ $? -eq 0 ]; then
      echo success   # ...and do something useful
      break
   fi
   echo fail
done < ipscrapers.txt

The key was reading Hanish's 2nd reply on this (old) page: http://www.cyberciti.biz/tips/howto-running-commands-from-a-variable.html


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 8 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:  
cron


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