How to search for PID? (bash)

Assuming I know the process PID and want to search in ps-A, how do I do this? I tried to do this:

echo "Enter PID to search: "
read PID
search=$(ps -A | grep -v PID | awk '{print $1}') 

This brings me back with a long list of PIDs. So, how can I use each individual output value and do:

if [ "$PID" = "*each_value_in_search_list*" ]; then
........

In this case, I am trying to compare what the user types with the output of my command, so how do I do this? Am I doing the right way first? Or is there any other way to do this?

Thanks for your help, everyone who answered this question. (

+3
source share
6 answers

-v grep , , , . , $.

#!/bin/bash

echo "Enter PID to search: "
read PID
search=$(ps --pid $PID -o comm=)

if [ $search ]
    then
        echo "Program: $search"
    else
        echo "No program found with PID: $PID"
fi
+4

ps , "PID". $PID , PID "PID". , , , PID, :

search=$(ps -A | grep "^ *$PID\>" | awk '{print $1}')

"\>" .

ps -p, PID, grep awk:

search=$(ps -p $PID)
+3

"grep -v PID" , , PID .

, , , :

search=$(ps -A | awk -v pid=$PID '$1==pid{print pid}')

PID, , , , .

PID ( 1 awk) , , PID .

+3

grep -p PID ps.

+3

If the root of the question is to check whether the entered PID matches the process entry (root). More specifically, if you can send a signal to this process (user):

if kill -0 $PID >/dev/null 2>&1; then ...

Of course, maybe this is not at all what you want. I was free to "slightly" interpret your question;)

+2
source
<?php
echo shell_exec("ps -Ao cmd,pid|grep -P ' {$pid}$'");
?>
0
source

Source: https://habr.com/ru/post/1709006/


All Articles