Skip to content

Latest commit

 

History

History
47 lines (39 loc) · 902 Bytes

68.md

File metadata and controls

47 lines (39 loc) · 902 Bytes

题目要求

给出一个进程PID,打印出该进程下面的子进程以及子进程下面的所有子进程。(只需要考虑子进程的子进程,再往深层次则不考虑)

参考答案

#!/bin/bash
#这个脚本用来打印子进程
#作者:猿课-阿铭 www.apelearn.com
#日期:2018-12-10

ps -elf > /tmp/pid.txt
read -p "Please input a pid: " p

if [ -z "$p" ]
then
    echo "你没有输入任何pid,请输入一个pid."
    exit
fi

if ! grep -qw "$p" /tmp/pid.txt
then
    echo "你输入的pid不存在."
    exit
fi

get_cpid()
{
    p1=$1
    ps -elf |awk -v p2=$p1 '$5==p2 {print $4}' > /tmp/$p1.txt
    n=`wc -l /tmp/$p1.txt|awk '{print $1}'`
    if [ $n -eq 0 ]
    then
	echo "进程$1下没有子进程."
    else
	echo "进程$1下的子进程是:"
	cat /tmp/$p1.txt
    fi
}

get_cpid $p

for c_p in `cat /tmp/$p.txt`
do 
    get_cpid $c_p
done