【发布时间】:2021-07-15 12:53:33
【问题描述】:
正如标题所说,我试图只回显超过 2600000 秒的进程,但它会回显 etime 小于 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 的 etime(不要注意到 PIDS_OVER... 列表):
PID :25271, Process owner :yonatanh, procces begin time: 2082286 (Seconds ago)
2082286 is bigger than 2600000
PID :25275, Process owner :yonatanh, procces begin time: 2082286 (Seconds ago)
2082284 is bigger than 2600000
PID :25299, Process owner :yonatanh, 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, Process owner :yonatanh, procces begin time: 6843 (Seconds ago)
2079327 is bigger than 2600000
PID :31324, Process owner :yonatanh, procces begin time: 2079327 (Seconds ago)
【问题讨论】:
-
尝试
$2 -gt 2600000进行数字比较 -
@markp-fuso 我收到错误第 19 行:[: ELAPSED: integer expression expected
-
另外,请注意您当前目录中的空文件
2600000?如果您确实想与>进行字符串比较,则必须将其转义以防止其被视为重定向运算符。[ $2 > 2600000 ]等价于[ $2 ] > 2600000。 -
这是一个极其常见的错误。我坚信解决该错误的一个简单方法是从不使用
[。当你把它写成if test $2 > 2600000时,错误似乎更明显,因为读者不会被错误地认为[和]是语法的一部分而感到困惑。