unix中字符串到整数的转换



我想打印进程id的内存。但是,我在if块中遇到了错误,因为它无法检查所需的整数字符串。

printf "%-10s%-15s%-15s%sn" "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%sn" "$PID" "$OWNER" "$MEMORY" "$COMMAND"
fi
done
}
sysmon_main | sort -bnr -k3

我的低于错误

./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

您可以使用以下脚本:


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%sn" "$pid" "$owner" "$mem" "$cmd"
fi
done < <(ps -o  pid,user,%mem,command ax)

相关内容

  • 没有找到相关文章

最新更新