1

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.

4
  • 1
    Another FAQ: Read about how to pass variables in awk. Commented Jan 23, 2014 at 12:31
  • Check your man page. ps should already have an option to output just the process IDs, without having to filter them out of the full output of ps -aux. Commented Jan 23, 2014 at 13:30
  • For the people who wants to close the question, the question isn't about how to use awk if you have a solution that doesn't use awk feel free to add. Commented Jan 23, 2014 at 21:39
  • @devnull are you saying that this answer could be answered only using awk? Commented Jan 23, 2014 at 21:59

2 Answers 2

4

This will do what you need:

pgrep -U $1
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

@LoïcFaure-Lacroix Because this was about in the question.But I am aware that l0b0's solution is better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.