I'm trying to write a script that involves storing a specific PID in a variable. I've got as far as: -
Code:
$PS_OUTPUT=ps -elf | grep <process name> | awk '{print $4}'
This stored the PID of the grep itself and the actual process I want. The output of $PS_OUTPUT is: -
I only want the second number as the first is the PID of the grep. I either want to suppress the PID of the grep from the output or just grab the second PID. Since the output is on two lines, I thought there must be a newline (\n) character in there somewhere, but if I use: -
Code:
ps -elf | grep <process name> | awk '{print $4}' | awk -F"\n" '{print $2}'
...I get nothing and {print $1} replaced in the above gives me both PIDs again on two lines. Which character is outputting the "ps" command on two lines that isn't a "\n"? Can I somehow write a regex that will exclude the PID of the grep itself?
Thanks for any help.