In the code below, the number 99 is hardcoded. How can I use $limit instead?
#!/bin/bash
limit=$((${1}+0))
if [ "$limit" -lt 1 ];
then :
limit=5
fi
echo `ps -eo "%c %C" | grep -v '%CPU' | grep -v '0.0' | sort | awk 'BEGIN{s="";}{if (s == $1){n=n+$2} else {if (s != ""){print s " " n};s=$1;n=$2}}END{if (s != ""){print s " " n;}}' | sort -rn -k +2 | awk 'BEGIN{p=0;} NR <= 99 {if(s==0&&$1!=""){s=1;print "\"hogs\":{"} if(s==1){if(NR != 1) { printf "," } printf " \"%s\":%.1f",$1,$2; }} END{if(s==1)print "}"}'`
Open in new window