Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Syntax/Rules (Can I do this?) Post 75877 by yongho on Wednesday 22nd of June 2005 04:08:26 PM
Old 06-22-2005
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 05:48 PM..
 

4 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

rules?

What rules? I have been searching for hours on the internet and just cannot seem to find the command you would type to add a serial port or the file that specifies whether a filesystem shoudl be mounted at boot time or not............. (1 Reply)
Discussion started by: Xskwizitboi
1 Replies

2. Post Here to Contact Site Administrators and Moderators

Rules

https://www.unix.com/showthread.php?t=2971 Spelling Error. You 'Adhere' to rules, not adhear. (2 Replies)
Discussion started by: Tux
2 Replies

3. Post Here to Contact Site Administrators and Moderators

rules

rules are there but asking 2 questions out of 30 is surely understanable esp when the instructor gives an open book test and urged us to seek answers anywhere we can except from him directly. (2 Replies)
Discussion started by: vrn
2 Replies

4. What is on Your Mind?

Forum rules

After reading the FAQ for the first time, I noticed Rule # 14 : :D:D:D Anyways, I hope to make more use of this forum in future, and also to help others. Regards Ook. :) (0 Replies)
Discussion started by: The_Librarian
0 Replies
flockfile(3S)															     flockfile(3S)

NAME
flockfile(), ftrylockfile(), funlockfile() - explicit locking of streams within a multithread application SYNOPSIS
DESCRIPTION
The and functions provide for explicit application-level locking of streams. These functions can be used by a thread to delineate a sequence of I/O statements that are to be executed as a unit. The function is used by a thread to acquire ownership of a object. The function is used by a thread to acquire ownership of a object if the object is available; is a non-blocking version of The function is used to relinquish the ownership granted to the thread. The behavior is undefined if a thread other than the current owner calls the function. Logically, there is a count associated with each stream. This count is implicitly initialized to zero when the stream is created. The stream is unlocked when the count is zero. When the count is positive, a single thread owns the stream. When the function is called, if the count is zero or if the count is positive and the caller owns the stream, the count is incremented. Otherwise, the calling thread is suspended, waiting for the count to return to zero. Each call to decrements the count. This allows matching calls to (or successful calls to and to be nested. All POSIX.1 and C standard functions that reference objects behave as if they use and internally to obtain ownership of these objects. RETURN VALUE
None for and The function returns zero for success and nonzero to indicate that the lock cannot be acquired. flockfile(3S)
All times are GMT -4. The time now is 02:44 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy