dustyblade
dustyblade

Reputation: 73

Using variables and multiple pips in bash?

I'm trying to finish up a maintenance script, and I'm getting caught up with the following line:

PUID=$(ps aux | grep $PID | grep -v $USER| cut -d' ' -f1)

I'm trying to pull a specific process ID ($PID) out of the ps aux command, (while ignoring the process the user just created with the same PID), then eliminate all but the user name of the process owner.

Currently, the command runs fine when run in the command line, however I've been having issues assigning it to the variable $PUID, or even just executing it as a command. Any advice?

EDIT:

I'm trying to get this figured out and I believe there is still a problem in passing the variable $PID, right now it's pulling from a file (which it does properly) using this line

PID=$(cat /nfs/pdx/home/komcconx/PID/current/pid)

and if I add an echo $PID it returns the proper pid.

when I run the command PUID=$(ps -p $PID -o uname=), I get an error saying "ERROR: Process ID list syntax error." and if I runn the command with a "1" in the place of $PID it properly returns "root"

any ideas?'

Final edit:

Found out the issue was the PID was being pulled from a DOS file, and I was trying to run the command with a DOS newline, this issue is closed!

Upvotes: 1

Views: 1075

Answers (1)

Sas
Sas

Reputation: 2503

If you only want the user name of a process you can use -o option:

PUID=$(ps -p $PID -o uname=);
echo $PUID

Upvotes: 2

Related Questions