[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Bash - Readline in nested whiles?



Olaf Reitmaier Veracierta wrote:
Mike Bird wrote:
On Sat March 7 2009 19:48:30 Olaf Reitmaier Veracierta wrote:
cat list.txt
a
b

while read r; do echo $r; done  < list.txt;
a
b

x=0; while read r; do while [ $x -lt 3 ]; do let x=$x+1; echo $r; done;
done  < lista.txt ;
a
a

Why not the output is? Can't be nested while when reading line?
a
a
a
b
b
bi

Any comment will be preciated.

while read r; do x=0; while [ $x -lt 3 ]; do let x=$x+1; echo $r; done;
done  < list.txt

--Mike Bird


Ok, fine Mike Bird.

But ... the real problem is bellow... and is not resolved with x=0 within nested while...

I mean the tries=0 bellow...

cat minutly.txt
# OCTOPUS_SMS_RF 2009-03-08 12:03:07
2 1
8 2

            instance=1
            while read line
            do
                echo $line
if [ ! -z "$line" ] && [ `echo $line | grep OCTOPUS_SMS_RF | wc -l` -eq 0 ]
                then
                    field1=`echo $line | cut -d" " -f 1`
                    field2=`echo $line | cut -d" " -f 2`
                    script="i${instance}m${field1}p${field2}.sh"
rm -f $script tries=0 while [ ! -f $script ] && [ $tries -lt 3 ]
                    do
wget "$url?action=gs&instance=$instance&monitor=$field1&parameter=$field2" -O $script
                        if [ $? -eq 0 ]
                        then
                            tries=3
                        else
                            let tries=$tries+1
                        fi
                    done
                fii
            done < minutly.txt

The ouput is...

# OCTOPUS_SMS_RF 2009-03-08 12:03:07
2 1
--2009-03-08 00:47:07-- http://localhost/octopus/client.php?action=gs&instance=1&monitor=2&parameter=1
Resolving localhost... 127.0.0.1, ::1
Connecting to localhost|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 322 [text/html]
Saving to: `i1m2p1.sh'

100%[==================>] 322 --.-K/s in 0s
2009-03-08 00:47:07 (24.4 MB/s) - `i1m2p1.sh' saved [322/322]

I can to a better code with | | | | but I don't understand why is doesn't work with nested whiles...


Hi Mike, thanks for your help.

I solved the problem... zooming on what cause exit the cycle and I found that there were accidentally some comparison depending on a nonexistent file ( if [ `cat ... ) because of this the silent fail the while exited.

Here is the new code ... with wget --tries= ...

while read line
do
  if [ `echo $line | grep OCTOPUS_SMS_RF | wc -l` -eq 0 ]; then
    field1=`echo $line | cut -d" " -f 1`
    field2=`echo $line | cut -d" " -f 2`
    script="i${instance}m${field1}p${field2}.sh"
    rm -f $script
wget --tries=3 "$url?action=gs&instance=$instance&monitor=$field1&parameter=$field2" -O $script if [ ! -e $script ] || [ `cat ${script} | grep OCTOPUS_SMS_RF | wc -l` -eq 2 ]; then
       echo "CLIENT: Error can't wget ${script}."
mail -s "OCTOPUS [ERROR] - $HOSTNAME - wget ${script}" $OCTOPUS_MAIL < ${script}
    fi
  fi
done < ${frequency}.txt

Regards.

--
----------------------------------------------------------------------
   "You don't know where your shadow will fall",
        Somebody.-
----------------------------------------------------------------------
 Ing. Olaf Reitmaier Veracierta <olafrv@gmail.com>
----------------------------------------------------------------------
 http://olafrv.googlepages.com (Personal Webpage)
----------------------------------------------------------------------


Reply to: