'How to use while loop comparing 2 variables

#!/bin/bash
touch dsa
n=`ps aux | wc -l`
i=2
while [ $i -le $n ];
do
        pid=`ps aux | awk 'NR==$i {print $2}'`
        ppid=`cat /proc/$pid/status | awk 'NR==7 {print $2}'`
        a=`cat /proc/1/sched | grep se.sum_exec_runtime | awk -F. '{print $2}' | awk '{print $3}'`
        b=`cat /proc/$pid/sched | grep nr_switches | awk '{print $3}'`
        avg=`expr $a / $b`
        echo "ProccesID=$pid:Parent_ProcessID=$ppid:Average_Time=$avg" > dsa
        i=$(( $i + 1 ))
done

    cat dsa | sort -nk 3

So, I'm trying to create a script which will get all processes's PID,PPID (from status file) and (avg=se.sum_exec_runtime/nr_switches) from sched file.

n - the number of all processes
i - counter
pid - pid of i-th process
ppid - ppid of i-th process
a - se.sum_exec_runtime
b- nr_switches
avg=se.sum_exec_runtime/nr_switches

Then I'll put it in dsa file - sort it - done. But it's ain't working It's something with my while or idk. What am I doing wrong? I'm getting something like this

cat: /proc//status: No such file or directory

So - it maybe cannot recognize my pid variable because my awk cannot recognize $i variable



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source