您的位置:首页 > 运维架构 > Shell

BASH练习第三课

2015-08-10 15:00 435 查看
写一个脚本,完成如下功能:

(1)显示当前系统上每一个进程及其进程号,而后提示用户输入一个进程号,如果用户输入的进程号错误,而说明其错误,并提醒用户重新输入;

(2)显示用户所选进程的state, PPID和RSS及期对应的值;

#!/bin/bash

exec 2>>/dev/null
#Phase.1 show all process,and get the PID from user
ps -ef
i=0
echo please enter the PID you want to query
while [ "$i" -ne 1 ];
do
read pid
ps -ef | awk '{print $2}' | grep -w $pid >/dev/null
if [ $? -eq 0 ];then
#echo $pid
let i=$i+1
else
echo the PID you input does not exist,please input the PID again
fi
done
#echo $i

#Phase.2 ouput the PID PPID RSS of the process which the PID that user input

STAT=` ps aux |grep  -w $pid  | grep -v grep | awk '{print $8}'`
PPID=` ps -ef |grep  -w $pid  | grep -v grep | awk '{print $3}'`
RSS=` ps aux |grep  -w $pid  | grep -v grep | awk '{print $6}'`

echo the process ID is $pid:STAT=$STAT,PPID=$PPID,RSS=$RSS
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: