"Sliding window" with variables


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers "Sliding window" with variables
# 1  
Old 04-05-2012
"Sliding window" with variables

I'm doing a little work that involves computing the average completion time of the last 5 of many file decompressions. It's not too tough, but I'm wondering if maybe there's a better way to write it. This is a bash script; here's the current idea:

Code:
ctime5=$ctime4
ctime4=$ctime3
ctime3=$ctime2
ctime2=$ctime1
ctime1=$time_current
sliding_average=$((($ctime5+$ctime4+$ctime3+$ctime2+$ctime1)/5))

So, that works, but is there a better way? Suppose I wanted to expand that to a window of 50 or 100? Many thanks in advance.
# 2  
Old 04-05-2012
Using arrays would be one way:

Code:
#!/usr/bin/env ksh
# add a value to the array
function add_value
{
    typeset i=0

    i=${#values[@]}             # current length
    if (( $i >= $nvalues ))     # cap at nvalues
    then
        i=$(( $nvalues - 1 ))
    fi
    for (( ; i > 0; i-- ))          # shift values up in the array
    do
        values[$i]=${values[$(($i-1))]}
    done

    values[0]=$1            # add the new value
}

# compute the mean
function mean
{
    typeset i=0
    typeset sum=0

    if (( ${#values[@]} < 1 ))   # prevent divide by zero accidents
    then
        echo 0
        return
    fi

    for (( i = 0; i < ${#values[@]}; i++ ))
    do
        sum=$(( ${values[$i]} + sum ))
    done

    echo $(( sum / ${#values[@]} ))
}

nvalues=5               # max number of values to maintain

add_value 25            # add values to the list
add_value 2
add_value 23
mean                    # compute mean at any time if needed
add_value 42   
add_value 43
add_value 44

mean                    # compute mean
exit


Should work in either bash or Kshell.
This User Gave Thanks to agama For This Post:
# 3  
Old 04-06-2012
Nice! Thanks for the help, and the direction on what to learn next.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Web Development

Attaching " Thank You " pop-up window in php form mail code

Dear Sir, I have got a php mail form code. Using this code, I want to use the JavaScript for " Thank You " pop-up window in the php form mail code. Where do I add the pop-up JavaScript for "thank you" ? My code is as follows: <?php //--------------------------Set these... (1 Reply)
Discussion started by: swapan
1 Replies

4. Solaris

Trap signal on Window Manager "X" button clicked?

Well, my first post... thanks in advance! Can applications be notified of the X Window close (with "X" button) so the signal handler can run a cleanup process method? About the app: built with GNU C/C++ on Solaris 10, with WxWidgets. It is launched by a shell script as a background task. The... (2 Replies)
Discussion started by: HandsOGold
2 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

Question about special variables: "-" and "$_"

both ksh/bash support this 2 special variables, Is there any document for reference? 1) "-" is $OLDPWD 2) "$_" is last argument of previous command. (4 Replies)
Discussion started by: honglus
4 Replies

7. Windows & DOS: Issues & Discussions

"Striping" the background of an Rxvt/Urxvt window in Cygwin

To get this: https://www.unix.com/members/silversleevesx-albums-incidental-shot-glass-picture127-termshot-rxvt-rootless.png out of Cygwin's rxvt, you have to tweak your /cygwin/etc/x11/app-defaults/rxvt file, which is here:... (0 Replies)
Discussion started by: SilversleevesX
0 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question