CommandPrograms.com

Terms and Conditions: All content of this site is for instructional purposes only and can be used ONLY AT YOUR OWN RISK - Notice given.

 

home   --> to commandprograms.com home page . . .

#!/bin/bash
# Close.sh
# list and or close running processes
nam=$1

if [ -z $nam ]; then
printf "%s\n" "USAGE: $(basename $0) [ <process name> or <process number> ]" ;
exit 1 ;
fi

printf "%s\n" "you entered: $nam"

# both of the following lines work, the first accepts PID's
numbers=$(ps -e --sort -pid | grep $nam | cut -b 1-5 ) ;
#numbers=$(pgrep $nam) ;

if [ -z "$numbers" ]; then
printf "%s\n" "process not found ..." ;
exit 1 ;
fi

printf "%s" "numbers:"
printf "%s " $numbers
printf "\n"

number=$(echo $numbers | cut -b 1-5) ;
name=$(ps -e --sort -pid | grep $number | cut -b 25- ) ;
num=$number ;

if [ -n $num ]; then
printf "%s\n" "found a process $name numbered: $num"
fi

num=$(echo $num | cut -b 1-5)
printf "%s" "$num $name is to be closed (Y/N): "

read Go

if [ 'y' = $Go ] || [ 'Y' = $Go ]; then
printf "%s\n" "OK"
kill $num
else
echo 'ABORT'
fi
#end#