The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM



View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 06-22-2005
yongho yongho is offline
Registered User
 

Join Date: Jun 2005
Location: New York City
Posts: 95
Syntax/Rules (Can I do this?)

I have a small piece of code that I need to iterate 24 times (for each hour of a day).... here's what it looks like now...

Code:
while read hour file; do
       
    if [ $hour -eq 00 ]; then
        count=`awk -F, 'BEGIN { count=0 } ............. { count++ } END { print count }' $file`
        hourly01=`expr $hourly1 + $trade_count`
    elif [ $hour -eq 01 ]; then
        count=`awk -F, 'BEGIN { count=0 } ............. { count++ } END { print count }' $file`
        hourly02=`expr $hourly2 + $trade_count`
    ..................
    fi

done < ls.txt
As you can see it's simple, and iterates over and over, but unneccessarily (24 times)... Could I replace all the numbers with one variable... like such..

don't take my code letter for letter, just take the idea and tell me if it can be done..

Code:
for (x=00; x<=23; x++) {
    if [ $hour -eq $x ]; then
        count=`awk -F, 'BEGIN { count=0 } ............. { count++ } END { print count }' $file`
        hourly$x=`expr ${hourly}$x + $trade_count`
    fi
}
Thats' the idea.. I know that I didn't use a unix for-loop, but I just started learning unix and I didn't check what the for looks like in unix yet).. But can I write the middle section (every occurence of $x) the way that I wrote it here?

Last edited by yongho; 06-22-2005 at 01:48 PM.
Reply With Quote
Forum Sponsor