如何解决BASH,如果语句未按预期运行
正如标题所说,我试图只回显早于 2600000 秒的进程,但它回显了 etimes 小于 2600000 的进程。
while read pro; do
set -- $pro
if [ $2 > 2600000 ]
then
echo $2 is bigger than 2600000
echo "
PID :$1,Process owner :$3,procces begin time: $2 (Seconds ago)
"
fi
PIDS_OVER_A_MONTH+=("PID:$2,Process owner:$2")
done < <(ps -eo pid,etimes,user )
这是我的输出,如您所见,它回显小于 2600000 的 etimes(不要注意 PIDS_OVER... 列表):
PID :25271,Process owner :yonatanh,procces begin time: 2082286 (Seconds ago)
2082286 is bigger than 2600000
PID :25275,procces begin time: 2082286 (Seconds ago)
2082284 is bigger than 2600000
PID :25299,procces begin time: 2082284 (Seconds ago)
7224 is bigger than 2600000
PID :29549,Process owner :it,procces begin time: 7224 (Seconds ago)
6843 is bigger than 2600000
PID :30225,procces begin time: 6843 (Seconds ago)
2079327 is bigger than 2600000
PID :31324,procces begin time: 2079327 (Seconds ago)
解决方法
几个推荐的更改:
- 使用
-gt
进行数值比较 - 添加
--no-headers
以取消ps
标题行 - 将
ps
值直接读入变量
把这一切放在一起:
while read -r pid elapsed owner
do
if [ "${elapsed}" -gt 2600000 ]
then
echo "${elapsed} is bigger than 2600000"
printf "\nPID : ${pid},Process owner : ${owner},procces begin time : ${elapsed} (Seconds ago)\n\n"
fi
PIDS_OVER_A_MONTH+=("PID:${pid},Process owner:${owner}")
done < <(ps --no-headers -eo pid,etimes,user )
,
你确实说过bash
,对吗?您需要可移植到其他解析器吗?
我会使用 bash
。
while read -r pid etimes user; do
if (( etimes > 2600000 )); then
echo "$etimes is bigger than 2600000"
printf "\nPID :%s,Process owner :%s,proccess begin time: %s (Seconds ago) \n\n" "$pid" "$user" "$etimes"
PIDS_OVER_A_MONTH+=("PID:$pid,Process owner:$user")
fi
done < <(ps -eo pid,user )
数字上下文 (( ))
非常清楚。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。