0

In the below program, I am able to print the ppid notable to print pid,

I want to know the answer, pid is important so I ndeed to print it

#! /bin/bash

echo " a.display PTD of parent and child "
echo " b. copy the file content "
echo " enter your choice "
read ch

case $ch in

a)
    echo " parent pid is $PPID"
    echo " pid is $PID"
;;

b)
    echo "enter a source file"
    read f1
    echo "enter destination file "
    read f2

    if [ -f $f1 ]
    then 
      cp $f1 $f2
else    
    echo "file does not exist"
fi
;;
esac
sam
  • 1
  • 1
  • 1
    Have you looked in these places: https://www.cyberciti.biz/faq/howto-display-process-pid-under-linux-unix/ https://www.cyberciti.biz/faq/how-to-return-pid-of-a-last-command-in-linux-unix/ https://stackoverflow.com/questions/2493642/how-does-a-linux-unix-bash-script-know-its-own-pid ? – Ina Jan 12 '22 at 12:42
  • I don't understand the requirements. Of *which* process do you want to print the PID? The process of the bash script itself is stored in `$$`. – user1934428 Jan 12 '22 at 13:09

2 Answers2

1

if you have sudo priveleges you can get process id using pgrep. see man pgrep

....  
a)
echo " parent pid is $PPID"

#you can get child id from parent process id.
pid=$(sudo pgrep --parent $PPID)
##now you have process id as pid

echo " pid is $pid"

b)...

*i don't have enough reputation to comment and answer with sudo

shnoq
  • 41
  • 5
1

You can always use $$ to get the PID.

a)
echo " parent pid is $PPID"
echo " pid is $$"
;;

I believe this is what you wanted to do.

Nicolas Kagami
  • 130
  • 1
  • 5