Sponsored Content
Top Forums UNIX for Dummies Questions & Answers "Sliding window" with variables Post 302619099 by agama on Thursday 5th of April 2012 12:28:05 AM
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:
 

8 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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
tswtclmt(7ipp)						   IP Quality of Service Modules					    tswtclmt(7ipp)

NAME
tswtclmt - Time Sliding Window Three Conformance Level Meter DESCRIPTION
The Time Sliding Window Three Conformance level meter (tswtcl) meters a traffic stream and determines the conformance level of its packets. Packets are deemed to belong to one of the three levels, Red, Yellow or Green, depending on the committed and peak rate. The meter provides an estimate of the running average bandwidth. It takes into account burstiness and smoothes out its estimate to approx- imate the longer-term measured sending rate of the traffic stream. The estimated bandwidth approximates the running average bandwidth of the traffic stream over a specific window (time interval). tswtcl estimates the average bandwidth using a time-based estimator. When a packet arrives for a class, tswtcl re-computes the average rate by using the rate in the last window (time interval) and the size of the arriving packet. The window is then slid to start at the current time (the packet arrival time). If the computed rate is less than the committed configuration parameter, the packet is deemed Green; else if the rate is less than the peak rate, it is Yellow; else Red. To avoid dropping multiple packets within a TCP window, tswtcl probabilistically assigns one of the three conformance level to the packet. STATISTICS
The tswtcl module exports global and per-class statistics through kstat: Global statistics: module: tswtclmt instance: <action id> name: tswtclmt statistics class <action name> green_bits <number of bit in green> green_packets <number of packets in green> red_bits <number of bits in red> red_packets <number of packets in red> yellow_bits <number of bits in yellow> yellow_packets <number of packets in yellow> FILES
/kernel/ipp/sparcv9/tswtclmt 64-bit module (SPARC only.) ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWqos | +-----------------------------+-----------------------------+ SEE ALSO
ipqosconf(1M), dlcosmk(7IPP), dscpmk(7IPP), flowacct(7IPP), ipqos(7IPP), ipgpc(7IPP), tokenmt(7IPP) RFC 2859, A Time Sliding Window Three Colour Marker (TSWTCM) W. Fang, N. Seddigh, B. Nandy -- The Internet Society, 2000 SunOS 5.10 29 Sep 2004 tswtclmt(7ipp)
All times are GMT -4. The time now is 08:22 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy