Reputation: 51
i want to print the memory of process id's. But, i am getting error in if block as it is unable to check for the string as integer is expected.
printf "%-10s%-15s%-15s%s\n" "PID" "OWNER" "MEMORY" "COMMAND"
function sysmon_main(){
RAWIN=$(ps -o pid,user,%mem,command ax |grep -v PID |awk '/[0-9]*/{print $1 ":" $2 ":" $4}')
for i in $RAWIN
do
PID=$(echo $i | cut -d: -f1)
OWNER=$(echo $i| cut -d: -f2)
COMMAND=$(echo $i| cut -d: -f3)
MEMORY=$(pmap $PID | tail -n 1| awk '/[0-9]K/{print $2}')
if [ "$MEMORY" -gt 0 ];then
printf "%-10s%-15s%-15s%s\n" "$PID" "$OWNER" "$MEMORY" "$COMMAND"
fi
done
}
sysmon_main | sort -bnr -k3
I'm getting below error
./sysmon: line 17: [: 0K: integer expression expected
./sysmon: line 17: [: 126704K: integer expression expected
./sysmon: line 17: [: 14216K: integer expression expected
./sysmon: line 17: [: 48187132K: integer expression expected
Upvotes: 0
Views: 127
Reputation: 784938
You may use this script:
while read -r pid owner m cmd; do
if [[ $pid =~ ^[0-9]+$ && $cmd != "ps -o"* ]]; then
mem="$(pmap $pid | awk 'END {print $NF+0}')"
((mem > 0)) && printf "%-10s%-15s%-15s%s\n" "$pid" "$owner" "$mem" "$cmd"
fi
done < <(ps -o pid,user,%mem,command ax)
Upvotes: 1