Register
It is currently Mon Oct 20, 2014 11:21 pm

Help with FOR loop variables


All times are UTC - 6 hours


Post new topic Reply to topic  [ 10 posts ] 
Author Message
 PostPosted: Tue Nov 23, 2010 9:03 am   

Joined: Tue Nov 23, 2010 8:52 am
Posts: 6
I've been struggling with this. The overall script worked, but since then I've removed a bunch of semi-redundant IFs in favor of a FOR loop.

What I have is several variables:

SRC1
SRC2
SRC3
SRC4

SRC1U
SRC2U
SRC3U
SRC4U


The loop needs to evaluate if each SRCx variable is set to 1, and if so, download based on the URL in SRCxU. Here's what I have, and I know the "$SRC$i" nonsense doesn't work (does it?)... But I don't know what does. I need to know how to evaluate each SRCx and how to download each SRCxU inside the FOR loop.

Code:
for i in {1..4}
do
   if [ "$SRC$i" = "1" ]  ; then
      if wget $SRC$iU -O - >> $DL ; then
         logger Downloaded $SRC$iU
      else
         logger Failed to download $SRC$iU
      fi
   fi
done


Top
 Profile  
 PostPosted: Tue Nov 23, 2010 2:21 pm   

Joined: Tue Nov 23, 2010 8:52 am
Posts: 6
Well I'll be dipped in doo-doo, it seems to work fine! I was so sure it wouldn't, I didn't even try initially. Oh well. If I'm missing something, please do let me know.


Top
 Profile  
 PostPosted: Tue Nov 23, 2010 3:18 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 669
Location: Des Moines, Iowa
he he he, welcome to bashscripts.

That was a first for me, someone asking the question and sure the code was wrong before even trying it.

Your just more talented than you thought ;) Glad it worked for you.


Top
 Profile WWW  
 PostPosted: Tue Nov 23, 2010 8:01 pm   

Joined: Tue Nov 23, 2010 8:52 am
Posts: 6
Thanks. :)

I'm having trouble with something else. I found that even if the download fails, the logger Failed to download $SRC$iU line doesn't get run. I don't understand why. But in any case, I tried redoing the whole approach:

Code:
for i in {1..4}
do
   if [ "$SRC$i" = "1" ]  ; then
      wget $SRC$iU -O - >> $DL
      if [ "$?" -eq 0 ] ; then
         logger Downloaded $SRC$iU
         eval GOTSRC="1"
      else
         logger Failed to download $SRC$iU
      fi
   fi
done

But now, the wget line doesn't run at all, and I can't figure out why.


Top
 Profile  
 PostPosted: Tue Nov 23, 2010 8:04 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 669
Location: Des Moines, Iowa
does the wget line have any spaces in the url it's downloading by chance ???

If so, try using some quotes to make sure it gets the file


Top
 Profile WWW  
 PostPosted: Tue Nov 23, 2010 9:22 pm   

Joined: Tue Nov 23, 2010 8:52 am
Posts: 6
No spaces, but I'm using this format:
Code:
eval SRC3U="http://www.malwaredomainlist.com/hostslist/hosts.txt"


Top
 Profile  
 PostPosted: Tue Nov 23, 2010 9:26 pm   

Joined: Tue Nov 23, 2010 8:52 am
Posts: 6
This piecemeal approach I'm using is ridiculous. Sorry for the stupidity. Here's the whole script.

The commented-out FOR loop works; the active one does not. Can someone tell me why?

Code:
#!/bin/sh

logger Hosts script: Script start

eval SLSEC="90"
logger Hosts script: Sleeping $SLSEC seconds...
sleep $SLSEC
logger Hosts script: Done sleeping

eval SRC1="1"
eval SRC1U="http://www.mvps.org/winhelp2002/hosts.txt"

eval SRC2="1"
eval SRC2U="http://pgl.yoyo.org/adservers/serverlist.php?mimetype=plaintext"

eval SRC3="1"
eval SRC3U="http://www.malwaredomainlist.com/hostslist/hosts.txt"

eval SRC4="1"
eval SRC4U="http://www.malwarepatrol.net/cgi/submit?action=list_hosts_win_127001"

eval PINGT1="google.com"
eval PINGT2="yahoo.com"

eval AUTOUP="1"

eval HOSTS="/tmp/hosts"

eval RDRIP="0.0.0.0"

if ping -c 1 -s 1 $PINGT1 ; then
   eval GDPING="1"
   logger Hosts script: Ping of $PINGT1 succeeded
else
   if ping -c 1 -s 1 $PINGT2 ; then
      eval GDPING="1"
      logger Hosts script: Ping of $PINGT1 failed
      logger Hosts script: Ping of $PINGT2 succeeded
   fi
fi

if [ "$GDPING" = "1" ] ; then

   eval GOTSRC="0"
   echo "" > $HOSTS

   for i in {1..4}
   do
      if [ "$SRC$i" = "1" ]  ; then
         wget $SRC$iU >> $HOSTS
         if [ "$?" -eq 0 ] ; then
            logger Hosts script: Downloaded $SRC$iU
            eval GOTSRC="1"
         else
            logger Hosts script: Failed to download $SRC$iU
         fi
      fi
   done

#   for i in {1..4}
#   do
#      if [ "$SRC$i" = "1" ]  ; then
#         if wget $SRC$iU -o - >> $HOSTS ; then
#            logger Hosts script: Downloaded $SRC$iU
#            eval GOTSRC="1"
#         else
#            logger Hosts script: Failed to download $SRC$iU
#         fi
#      fi
#   done

   if [ "$GOTSRC" = "0" ]; then
      logger Hosts script: Failed to download any Hosts files
   fi

   if [ "$GOTSRC" = "1" ]; then

      logger Hosts script: Restarting dnsmasq
      service dnsmasq stop
      killall -9 dnsmasq

      sed -i -e '/^[0-9A-Za-z]/!d' $HOSTS
      sed -i -e '/%/d' $HOSTS
      sed -i -e 's/[[:cntrl:][:blank:]]//g' $HOSTS
      sed -i -e 's/^[ \t]*//;s/[ \t]*$//' $HOSTS
      sed -i -e 's/[[:space:]]*\[.*$//'  $HOSTS
      sed -i -e 's/[[:space:]]*\].*$//'  $HOSTS
      sed -i -e '/[[:space:]]*#.*$/ s/[[:space:]]*#.*$//'  $HOSTS
      sed -i -e '/^$/d' $HOSTS
      sed -i -e '/127.0.0.1/ s/127.0.0.1//'  $HOSTS
      sed -i -e '/^www[0-9]./ s/^www[0-9].//'  $HOSTS
      sed -i -e '/^www./ s/^www.//' $HOSTS

      cat $HOSTS | sort -u > $HOSTS.new
      mv $HOSTS.new $HOSTS

      sed -i -e 's|$|/'$RDRIP'|' $HOSTS
      sed -i -e 's|^|address=/|' $HOSTS

      cat /etc/dnsmasq.conf >> $HOSTS

      ## whitelist

      sed -i -e '/highcriteria.ca/d' $HOSTS
      sed -i -e '/rapidshare.com/d' $HOSTS
      sed -i -e '/regnow.com/d' $HOSTS
      ##sed -i -e '/whitelist4.com/d' $HOSTS
      ##sed -i -e '/whitelist5.com/d' $HOSTS
      ##sed -i -e '/whitelist6.com/d' $HOSTS
      ##sed -i -e '/whitelist7.com/d' $HOSTS
      ##sed -i -e '/whitelist8.com/d' $HOSTS
      ##sed -i -e '/whitelist9.com/d' $HOSTS
      ##sed -i -e '/whitelist10.com/d' $HOSTS

      dnsmasq --conf-file=$HOSTS

      logger Hosts script: Attempting to start dnsmasq
      dnsmasq

      logger Hosts script: Unique Hosts entries: $(awk 'END { print NR }' $HOSTS)

   fi

else

   logger Hosts script: Ping of $PINGT1 and $PINGT2 failed
   logger Hosts script: Hosts files will not be downloaded

fi

rm $HOSTS*

if [ "$AUTOUP" = "1" ] ; then
   if [ -x /tmp/script_wanup.sh ] ; then
      logger Hosts script: Auto-update enabled
      cru a UpdateHostsFiles "0 0 * * * /tmp/script_wanup.sh >/dev/null 2>&1"
   fi
else
   logger Hosts script: Auto-update disabled
   cru d UpdateHostsFiles
fi

logger Hosts script: Script end

# end


Top
 Profile  
 PostPosted: Fri Nov 26, 2010 4:04 pm   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
You do not have to use eval to set a variable.
Your testing does not work, see this example:
Code:
SRC1=http://www.google.com
SRC2=https://mail.hotmail.com
for i in 1 2 ; do
  echo "$SRC$i"
done

However, using
Code:
eval echo "\$SRC$i"

works fine.
Using bash's arrays would make your code easier to both write and read.


Top
 Profile  
 PostPosted: Mon Nov 29, 2010 7:05 am   

Joined: Tue Nov 23, 2010 8:52 am
Posts: 6
Thank you very much, choroba. Your advice was somewhat cryptic to this idiot, but still helpful. :) I think I've nailed it.

I found that Tomato doesn't like for i in {1..4}. I guess it doesn't have the latest version of Bash. Whatever. This does work:

Code:
for i in 1 2 3 4
   do
      eval SRC=\$SRC$i
      eval SRCU=\$SRCU$i
      if [ "$SRC" = "1" ]  ; then
         wget $SRCU -O - >> $HOSTS
         eval RETCODE=$?
         if [ "$RETCODE" -eq 0 ] ; then
            logger Hosts script: Downloaded $SRCU
            GOTSRC="1"
         else
            logger Hosts script: Failed to download $SRCU \(Return code $RETCODE\)
         fi
      fi
   done

I had to rename the SRC1U, SRC2U, etc. variables to SRCU1, SRCU2, etc. to make them work in the FOR loop. I'm sure there is a way to make them work in their original form, but I couldn't easily find it. I also found (after some difficulty) that WGET is case-sensitive. It didn't like "-o" at all. It had to be "-O". The irritating thing is that my WGET docs show "-o", not "-O".

Thanks again for helping when no one else would.


Top
 Profile  
 PostPosted: Thu Dec 02, 2010 1:49 am   

Joined: Thu Dec 02, 2010 1:44 am
Posts: 1
tired wrote:
I'm sure there is a way to make them work in their original form, but I couldn't easily find it.

keep your original form and try this
Code:
    eval SRCU="\$SRC$i"U


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 4 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:  


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