arrays - 错误的数组下标

标签 arrays bash shell

我对 Shell 比较陌生,我一直在尝试在 Shell 中使用数组,但效果不佳。如果 OpenVZ 容器在一段时间内达到特定带宽阈值,我正在尝试编写一个脚本来暂停它们。

我的脚本:

#!/bin/bash
#Thresholds are in bytes per second and count1 must > count2
LOGDIR="/var/log/outbound_ddos"
SECS=10
THRESHOLD1=65536000
COUNT1=10
THRESHOLD2=117964800
COUNT2=2

while [ 1 ]
do
    for veid in $(/usr/sbin/vzlist -o veid -H)
    do      
        # Create the log file if it doesn't already exist
        if ! test -e $LOGDIR/$veid.log; then
            touch $LOGDIR/$veid.log
        fi

        # Parse out the inbound/outbound traffic and assign them to the corresponding variables     
        eval $(/usr/sbin/vzctl exec $veid "grep venet0 /proc/net/dev"  |  \
            awk -F: '{print $2}' | awk '{printf"CTOUT=%s\n", $9}')

        # Print the output and a timestamp to a log file
        echo $(date +%s) $CTOUT >> $LOGDIR/$veid.log

        # Read last 10 entries into arrays
        i=0
        tail $LOGDIR/$veid.log | while read time byte
        do
            times[$i]=time; bytes[$i]=byte
            let i++
        done

        # Sanity checks and calculations
        for (( i=0; i<COUNT1; i++ ))
        do
            # Sanity check on the time
            if (( times[$i] > times[$i - 1] + $SECS + 10 ))
                then continue
            fi

            # Calculate differences
            let "diff[$i] = bytes[$i+1] - bytes[$i]"
        done

        # Work out thresholds and suspend where necessary
        # Higher threshold first
        t2=0
                for (( i=COUNT1-1; i>=0; i-- ))
                do
                        if [ ! diff[$i] ]
                                then continue
                                else
                                        if (( bytes[$i] < THRESHOLD1 * SECS + bytes[$i-1] + 1 ))
                                                then continue
                        else let t2++
                                        fi
                        fi
                done
        #Lower threshold last
        t1=0
        for (( i=COUNT1-1; i>=0; i-- ))
        do
            if [ ! diff[$i] ]
                then continue
                else
                    if (( bytes[$i] < THRESHOLD1 * SECS + bytes[$i-1] + 1 ))
                        then continue
                        else let t1++
                    fi
            fi
        done

        # Suspend where necessary
        if (( ($t2 >= $COUNT2) || ($t1 >= $COUNT1) ))
            then vzctl stop veid
        fi
    done
    sleep $SECS
done

错误:

script.sh: line 38: times: bad array subscript
script.sh: line 54: bytes: bad array subscript
script.sh: line 67: bytes: bad array subscript

我已经尝试了数组下标的几种变体,但我似乎无法摆脱这些错误。

最佳答案

所以在第 38 行,

if (( times[$i] > times[$i - 1] + $SECS + 10 ))

在迭代期间将引用 times[-1] 一次。负指数是 only very recently part of bash arrays ,所以这很可能是您收到错误的原因。

与第 54 行和第 67 行一样,您遇到了一次负数数组下标。调整循环以避免 [0 - 1]

关于arrays - 错误的数组下标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16120155/

相关文章:

c# - .NET 核心 : Array class does not contain definition for ConvertAll

bash - "key=value bash-script"用法记录在哪里?

bash - 如何写入 Bash 中的命名文件描述符?

arrays - 为什么空数组在 bash 中被视为未设置?

java - 将 System.out 发送到文件和 Commons Logging

linux - 有没有办法测量 shell 命令使用了多少内存?

java - 比较两个数组时,从第三个数组打印正确的项目时出现问题

javascript - 从 map 外部动态显示传单标记弹出窗口

php - 如何将段落更改为 PHP 中的数组,包括空格和标点符号

linux - 不必记住我要 pull 和推的分支的最佳别名是什么?