1

i have multiples arrays populated with data, and now i want to print each element of those arrays but i´m getting always the same value for all. This is my code:

#!/bin/bash
hertz=$(getconf CLK_TCK)
PIDS=$(ls -la /proc | awk '{print $9}' | grep "^[0-9]*$")
PIDLIST=$(echo $PIDS | tr "" "\n")
counter=0
declare -A USER
declare -A PRIORITY
declare -A VIRTUALMEMORY
declare -A CPUUSAGE
declare -A MEMORY
declare -A NAME
declare -A TIME

printf "%-10s %-10s %-10s %-10s %-10s %-10s %-10s %-11s %-10s\n" PID USUARIO PR VIRT S %CPU %MEM TIME COMMAND

for PID in $PIDLIST; do
    if [ -d /proc/$PID ]; then
        #--CALCULATE USER--#
        useruid=$(awk '/Uid/ {print $2}' 2> /dev/null < /proc/$PID/status)
        user=$(getent passwd $useruid | cut -d: -f1)
        #--CALCULATE PRIORITY--#
        PRIORITY[$counter]=$(awk '{print $18}' 2> /dev/null < /proc/$PID/stat)
        #--CALCULATE VIRTUAL MEMORY--#
        VIRTUALMEMORY[$counter]=$(awk '{print $23}' 2> /dev/null < /proc/$PID/stat)
        #--CALCULATE STATE--#
        STATE[$counter]=$(awk '{print $3}' 2> /dev/null < /proc/$PID/stat)
        #--CALCULATE CPU TIME--#
        #-- user_util = 100 * (utime_after - utime_before) / (time_total_after - time_total_before); --#
        uptime=$(awk '{print $1}' < /proc/uptime)
        utime=$(awk '{print $14}' < /proc/$PID/stat)
        stime=$(awk '{print $15}' < /proc/$PID/stat)
        cutime=$(awk '{print $17}' < /proc/$PID/stat)
        cstime=$(awk '{print $16}' < /proc/$PID/stat)
        starttime=$(awk '{print $22}' < /proc/$PID/stat)
        totaltime=$(($utime+$stime+$cutime+$cstime))
        middlevalue=$((starttime / hertz))
        seconds=$(echo "scale = 2; $uptime-$middlevalue" | bc)
        middlevalue=$(echo "scale = 2;$totaltime/$hertz" | bc)
        middlevalue=$(echo "scale = 3;$middlevalue/$seconds" | bc)
        CPUUSAGE[$counter]=$(echo "scale = 2; $middlevalue*100" | bc | awk '{printf "%.2f", $0}')
        #--CALCULATE %MEMORY--#
        MEMORY[$counter]=$(echo 0 $(awk '/Pss/ {print "+", $2}' 2> /dev/null < /proc/$PID/smaps) | bc)
        #--CALCULATE TIME--#
        TIME[$counter]=$(date -d@$seconds -u +%H:%M:%S)
        #--CALCULATE NAME--#
        NAME[$counter]=$(awk '{print $1}' 2> /dev/null < /proc/$PID/cmdline)
        counter=$((counter + 1))
        printf "%-10s %-10s %-10s %-10s %-10s %-10s %-10s %-11s %-10s\n" $PID $user "$PRIORITY[$counter]" "$VIRTUALMEMORY[$counter]" "$STATE[$counter]" "$CPUUSAGE[$counter]" "$MEMORY[$counter]" "$TIME[$counter]" "$NAME[$counter]"
    fi

done

And this is my output.

PID        USUARIO    PR         VIRT       S          %CPU       %MEM       TIME        COMMAND
1          root       20[-1]     3756032[-1] S[hB1]     0.10[-1]   0[-1]      21:22:27[-1] /sbin/init[-1]
10         root       20[-1]     3756032[-1] S[hB1]     0.10[-1]   0[-1]      21:22:27[-1] /sbin/init[-1]
11         root       20[-1]     3756032[-1] S[hB1]     0.10[-1]   0[-1]      21:22:27[-1] /sbin/init[-1]
1140       root       20[-1]     3756032[-1] S[hB1]     0.10[-1]   0[-1]      21:22:27[-1] /sbin/init[-1]
1145       root       20[-1]     3756032[-1] S[hB1]     0.10[-1]   0[-1]      21:22:27[-1] /sbin/init[-1]
1167       root       20[-1]     3756032[-1] S[hB1]     0.10[-1]   0[-1]      21:22:27[-1] /sbin/init[-1]
12         root       20[-1]     3756032[-1] S[hB1]     0.10[-1]   0[-1]      21:22:27[-1] /sbin/init[-1]

As you can see i'm having always same value for array elements. What´s wrong?

1 Answer 1

2

You need some curly braces in the printf statement: ${PRIORITY[$counter]}, etc. Also increment counter after the printf rather than before.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.