Register
It is currently Thu Nov 27, 2014 1:26 pm

can someone make a while loop for me


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Tue Jun 28, 2011 4:43 pm   

Joined: Tue Jun 28, 2011 4:38 pm
Posts: 7
Hi,

very new to bash scripting. hacking my way through a pretty big program.

anyway the $go could be $go1-$go9

in the below example there are 4 $gos

how could I create a while loop instead of doing the below for each instance of go.

Code:
if [ $go1 == "" ]
   then
   echo -e "do nothing"
   else
   echo -e "$go1" >> myfile
fi

if [ $go2 == "" ]
   then
   echo -e "do nothing"
   else
   echo -e "$go2" >> myfile
fi


if [ $go3 == "" ]
   then
   echo -e "do nothing"
   else
   echo -e "$go3" >> myfile
fi

if [ $go4 == "" ]
   then
   echo -e "do nothing"
   else
   echo -e "$go4" >> myfile
fi


Thanks

Dynacade


Top
 Profile  
 PostPosted: Tue Jun 28, 2011 5:30 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
That was something funny to do (with indirections).

Code:
while ((i++ < 9)); do
    printf -v go go$i

    if [[ ! ${!go} ]]; then
        echo 'do nothing'
    else
        echo "${!go}" >> myfile
    fi 
done


Top
 Profile  
 PostPosted: Tue Jun 28, 2011 5:54 pm   

Joined: Tue Jun 28, 2011 4:38 pm
Posts: 7
Thanks jsz...

Yes I am would be blushing if anyone saw my code. I do things very silly.. but it gets done :)

I am going to try your loop!

Thanks for your help. I will let you know if it worked.


Top
 Profile  
 PostPosted: Tue Jun 28, 2011 6:33 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Well, you just thought of another way to do it.

By the way, I think that do nothing doesn't have to be printed, does it?
If so, you can reduce the code:

Code:
while ((i++ < 9)); do
    printf -v go go$i

    if test "${!go}"; then
        echo "$_" >> myfile
    fi
done


Also, I'd use a for-loop instead of a while-loop since you can declare the variable i explicitly within it, and it's more understandable.

Code:
for ((i = 1; i <= 9; i++)); do
    printf -v go go$i

    if test "${!go}"; then
        echo "$_" >> myfile
    fi 
done


Top
 Profile  
 PostPosted: Wed Jun 29, 2011 12:05 pm   

Joined: Tue Jun 28, 2011 4:38 pm
Posts: 7
eeek.. Ok so I am still having a problem.. So maybe I should explain a little further what I am doing.

Yes "Do nothing" was just something I put in there so the forum would know I am not doing anything there.

I have have a script that runs date, time and wget commands 1-9 times in a row over ssh. It records the results to a .txt file

Code:
ssh User@myserver.net "( time wget -o tmp/wgetlog $accelurl -O /dev/null ) 2> tmp/timelog"
timestamp=`User@myserver.net date +"%r"`
echo -e "Time location: $timestamp" >> myresults

ssh User@myserver2.net "( time wget -o tmp/wgetlog $accelurl -O /dev/null ) 2> tmp/timelog"
timestamp=`User@myserver2.net date +"%r"`
echo -e "Time location: $timestamp" >> myresults2

ssh User@myserver3.net "( time wget -o tmp/wgetlog $accelurl -O /dev/null ) 2> tmp/timelog"
timestamp=`User@myserver3.net date +"%r"`
echo -e "Time location: $timestamp" >> myresults3


the above example is a run of 3 times to 3 servers. as I said this could be run 1-9 times.


I then have some grep | cut commands that pull the times and speeds out and writes them to >> result.file

so then I run

Code:
for l in `cat /var/www/jobs/$filename/result.file | grep -A3 Accelerated | grep results | | cut -d: -f2 |sed 's/ //g'`
                        do
                        eval "time$c='$l'"
                        c=$(( $c + 1 ))
                        done


This will give me:

$time1
$time2
$time3

inside the variables is

$time1=0m0.623s
$time2=0m0.738s
$time3=0m0.654s

I then do the same thing for speed

$speed1=567kbs
$speed2=435kbs
$speed3=583kbs

Now I want to write to a spread sheet

Code:
                echo -e "<Row ss:AutoFitHeight="0">" >> $filename.xml
                echo -e "<Cell ss:StyleID="s63"><Data ss:Type="String">URL Tester</Data></Cell>" >> $filename.xml
                echo -e "<Cell ss:StyleID="s63"/>" >> $filename.xml
                echo -e "<Cell ss:StyleID="s63"/>" >> $filename.xml
                echo -e "<Cell ss:StyleID="s63"/>" >> $filename.xml
                echo -e "<Cell ss:StyleID="s63"/>" >> $filename.xml
                echo -e "</Row>" >> $filename.xml


Now since I have 3 results I need to write a new row 3 times

here is an example of a filled out row

Code:
                echo -e "<Row ss:AutoFitHeight="0">" >> $filename.xml
                echo -e "<Cell ss:StyleID="s62"><Data ss:Type="String">$jobname</Data></Cell>" >> $filename.xml
                echo -e "<Cell ss:StyleID="s62"><Data ss:Type="String">$accellurl</Data></Cell>" >> $filename.xml
                echo -e "<Cell ss:StyleID="s62"><Data ss:Type="String">$date-at-site1</Data></Cell>" >> $filename.xml
                echo -e "<Cell ss:StyleID="s62"><Data ss:Type="String">$time1</Data></Cell>" >> $filename.xml
                echo -e "<Cell ss:StyleID="s62"><Data ss:Type="String">$speed1</Data></Cell>" >> $filename.xml
                echo -e "</Row>" >> $filename.xml


So what I want to do is be able to repeat/append the above for each time there is $time1-9 or $speed1-9.

Hopefully that made sense.. I am kind of stuck on this. So any help will be greatly appreciated... :)

All the best
Cade


Top
 Profile  
 PostPosted: Sat Jul 09, 2011 7:38 am   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 673
Location: Des Moines, Iowa
Well, I'm not sure if your stuck on creating the spreadsheet part or the other part, but I deal with spreadsheets/bash everday.
What I've found easiest for me, is to normally write the data out to a txt file, in a pipe delimited fashion.

Importing pipe delimited data into a spreadsheet is extremely easy to accomplish. Exporting the data is equally as simple.

This page: http://www.oooforum.org/forum/viewtopic.phtml?t=12922 has alot of examples of converting from one format to another if you have to do it via the script.


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [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