Display current directory for a running process for script

I'm trying to create a ksh script to do the following:

1) Ask the user what process they want to search for.
2) Have the script read the input as a variable and use it to search for the process.
3) Display the current time & date, the working directory of the process, and finally display the information for only that process.

Ideal script output to the user:

current date here

1390:       /home2/TEST/
root 1390        1   0   May 10      - 11:02 phantom EBC

Here is how I would manually perform the task:

# ps -ef | grep EBC
    root 1390        1   0   May 10      - 11:02 phantom EBC
    root 1652        1   0   Apr 17      -  0:22 phantom EBC
    root 1884        1   0   Jun 04      -  4:43 phantom EBC
    root 1912        1   0   Jun 22      -  7:08 phantom EBC
    root 2314 3134   0 12:05:48  pts/3  0:00 grep EBC
    root 2698        1   0   Apr 17      - 10:51 phantom EBC

# procwdx 1390
1390:       /home2/TEST/

This is what I have so far but I know its not right, Please help :confused:

#!/bin/ksh

echo -n "Enter process name:"
read Answer
for i in `ps -ef | grep EBC | grep -v grep | awk '{print $2}'`;do
        procwdx $i | awk '{print $1 $Answer}'| ps -ef | grep $1
done

echo $(date +'%D %T')

How do you want to determine THE process? If the user enters "phantom EBC", there's five of them to select from... And, how do you call the script?

If theres a way where the user enters TEST which would be the working directory for pid 1390.

Here we only prompt for directory if more than 1 process matches string.

As your probably on AIX, grep will most likely not support -w if that's the case just use -q instead of -wq.

printf "Enter process name: "
read Answer

count=`ps -eo pid,comm | grep "$Answer" | wc -l`

if [ $count -gt 1 ]
then
   printf "\nMore that 1 process found\n"
   printf "Enter process folder: "
   read Folder
fi

ps -eo pid,comm | grep "$Answer" | while read pid command
do
   ln=`procwdx $pid`
   echo "$ln" | grep -wq "${Folder}" && echo "$ln"
done

Thanks Chubler XL!
Yes I'm currently working on aix os. However, when I use the command ps -eo pid,comm it isnt returning anything.

try ps -eo pid,cmd or ps -e -o pid,args

Thank you Chubler_XL! I was able to get the script to work. Only having one little issue with my script reading the minus sign " - " as is and not an argument. Any suggestions will be greatly appreciated.

---------- Post updated at 04:54 AM ---------- Previous update was at 04:23 AM ----------

actually nevermind I found my mistake :slight_smile: