0

The code I have now

line=$(ps -A|awk '/$name/{print $1}')

doesn't seems working. What I want is use awk to search ps's output by process name and return the pid within my script

1
  • Often when pattern matching against ps(1), it helps to restrict the output to just those fields of interest, so as not to accidentally match against usernames or ttynames or whatever. Try ps -Awo pid=,comm= on your system. Commented Apr 19, 2013 at 17:30

2 Answers 2

4
line=$(ps -A|awk -v name="$name" '$0 ~ name{print $1}')

See here for the right ways to access the values of shell variables in awk scripts.

Sign up to request clarification or add additional context in comments.

Comments

1
line=$(ps -A|awk '/'$name'/{print $1}')

You are missing extra quote ' for $name

1 Comment

No. Never do this, it is unnecessary and will lead to cryptic errors in future.

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.