BashScripts.org
http://bashscripts.org/forum/

Help with FOR loop variables
http://bashscripts.org/forum/viewtopic.php?f=8&t=1253
Page 1 of 1

Author:  tired [ Tue Nov 23, 2010 9:03 am ]
Post subject:  Help with FOR loop variables

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

Author:  tired [ Tue Nov 23, 2010 2:21 pm ]
Post subject:  Re: Help with FOR loop variables

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.

Author:  crouse [ Tue Nov 23, 2010 3:18 pm ]
Post subject:  Re: Help with FOR loop variables

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.

Author:  tired [ Tue Nov 23, 2010 8:01 pm ]
Post subject:  Re: Help with FOR loop variables

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.

Author:  crouse [ Tue Nov 23, 2010 8:04 pm ]
Post subject:  Re: Help with FOR loop variables

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

Author:  tired [ Tue Nov 23, 2010 9:22 pm ]
Post subject:  Re: Help with FOR loop variables

No spaces, but I'm using this format:
Code:
eval SRC3U="http://www.malwaredomainlist.com/hostslist/hosts.txt"

Author:  tired [ Tue Nov 23, 2010 9:26 pm ]
Post subject:  Re: Help with FOR loop variables

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

Author:  choroba [ Fri Nov 26, 2010 4:04 pm ]
Post subject:  Re: Help with FOR loop variables

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.

Author:  tired [ Mon Nov 29, 2010 7:05 am ]
Post subject:  Re: Help with FOR loop variables

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.

Author:  universe-traveller [ Thu Dec 02, 2010 1:49 am ]
Post subject:  Re: Help with FOR loop variables

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

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group • http://www.phpbb.com