BASH,if 语句未按预期运行
BASH, if statement not operating as expected
正如标题所说,我试图仅回显早于 2600000 秒的进程,但它回显 etimes 小于 2600000 的进程。
while read pro; do
set -- $pro
if [ > 2600000 ]
then
echo is bigger than 2600000
echo "
PID :, Process owner :, procces begin time: (Seconds ago)
"
fi
PIDS_OVER_A_MONTH+=("PID:, Process owner:")
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, 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)
一些建议的更改:
- 使用
-gt
进行数值比较
- 添加
--no-headers
以抑制 ps
header 行
- 将
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,etimes,user )
数字上下文 (( ))
非常清楚。
正如标题所说,我试图仅回显早于 2600000 秒的进程,但它回显 etimes 小于 2600000 的进程。
while read pro; do
set -- $pro
if [ > 2600000 ]
then
echo is bigger than 2600000
echo "
PID :, Process owner :, procces begin time: (Seconds ago)
"
fi
PIDS_OVER_A_MONTH+=("PID:, Process owner:")
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, 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)
一些建议的更改:
- 使用
-gt
进行数值比较 - 添加
--no-headers
以抑制ps
header 行 - 将
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,etimes,user )
数字上下文 (( ))
非常清楚。