思路: ps -elf查看进程ppid根据ppid赛选pid进程 #!/bin/bash #The script is used to print child processes. #Date 2021-01-16 file=/tmp/pid.txt #定义文本路径 while : do read -p "please input a pid: " pid if [ -z $pid ];then#判断是否为空 echo "The input can not be empty (input q or quit to exit)." continue elif [ $pid == "q" ]||[ $pid == "quit" ];then#退出循环 echo "exit." exit fi ps -elf| awk -v pid2=$pid '$5==pid2 {print $0}'>$file#筛选pid if ! grep -qw "$pid" $file;then#过滤pid echo "The input pid does not exist (input q or quit to exit)." continue fi break done child_pid() { ps -elf|awk -v pid_new=$1 '$5==pid_new {print $4}' >$1.txt #将进程id写入文本 n=`wc -l $1.txt| awk '{print $1}'` if [ $n -eq 0 ];then echo "There are no subprocess under the pid $1" elif [ $n -ne 0 ];then echo "The pid $1 subprocess according to following:" cat $1.txt fi }#定义筛选函数 child_pid $pid for i in `cat $pid.txt` #for循环将每一个子进程包含的子进程筛选出 do child_pid $i done for i in `cat $pid.txt` #for循环遍历删除文本 do [ -f $i.txt ] && rm $i.txt done [ -f $pid.txt ] && rm $pid.txt#删除文本