LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   summinf values in bash (https://www.linuxquestions.org/questions/linux-newbie-8/summinf-values-in-bash-4175431491/)

julirio 10-10-2012 08:33 AM

summinf values in bash
 
hi! i'm trying to get all the regular files opened by all the processes in the current session. I have this code

while read pid
do

FILES_ACTUAL=$(lsof -p $pid | grep REG | wc -l)


done < <(ps -o pid,tt -u $USER | grep $CURRENT_TERMINAL | awk '{print $1}')

echo $FILES_ACTUAL

but i dont know how to add up, inside the while, the variable $FILES_ACTUAL.. I tried to use the awk command, but i couldnīt do it.

pan64 10-10-2012 08:42 AM

Code:

SUM=0
while read pid
do

FILES_ACTUAL=$(lsof -p $pid | grep REG | wc -l)
SUM=$(( SUM + FILES_ACTUAL ))

done < <(ps -o pid,tt -u $USER | grep $CURRENT_TERMINAL | awk '{print $1}')

echo $FILES_ACTUAL
echo $SUM

something like this may help

grail 10-10-2012 09:03 AM

Some other suggestions for your code:
Code:

SUM=0
while read pid
do

FILES_ACTUAL=$(lsof -p $pid | grep -c REG)
(( SUM ++ FILES_ACTUAL ))

done < <(ps -o pid,tt -u $USER | awk -vct="$CURRENT_TERMINAL" '$2 ~ ct{print $1}')

echo $FILES_ACTUAL # not sure the point of showing the last value this was set to??
echo $SUM



All times are GMT -5. The time now is 08:28 PM.