Marcin Majewski
Marcin Majewski

Reputation: 1047

Writing script to find all PID of processes of user

I really want to use ps -aux to get this. So I wrote script:

#!/bin/bash
if [ $# -lt 1 ]; then
        echo -n "No arguments were written"
        read uid
else
        uid=$1
fi
procesy=`ps -aux | awk '{if ($1=="$uid") print$2}'`
echo $procesy

Why it is not working ? When I am writinig ./script root I am getting nothing but blank line.

Upvotes: 1

Views: 168

Answers (2)

Radu Rădeanu
Radu Rădeanu

Reputation: 2722

In your script you have a problem using a shell variable inside awk. The right way using awk should be something like:

#!/bin/bash
if [ $# -lt 1 ]; then
        echo -n "No arguments were written"
        read user
else
        user=$1
fi
procesy=`ps aux | awk -v usr=$user '{if ($1==usr) print$2}'`
echo $procesy

Upvotes: 1

l0b0
l0b0

Reputation: 58828

This will do what you need:

pgrep -U $1

Upvotes: 4

Related Questions