0

I want to write the PIDs of some of the running processes into an array for further use.

Function()
{

    declare -a arr

    ps -e -o pid,%mem,command |

    awk -v val="$1" '{if($3 == val) print $1,$2,$3; $arr+=($1)}'
}

It seems I can't have access to arrays/variables inside awk.

I'd appreciate any help!

  • 1
    Hello PHP. Can you please provide an example of the ps -e o pid,%mem,command output you're wanting to process, explain which fields you want to extract (and under what criteria), and what your desired end state should be. – roaima May 15 at 16:42
  • @roaima Ok, the program has to provide a search engine for the running processes. When searching is done, the user is asked whether he/she wants to kill the found processes or not. If says yes, all the processes that have already been printed shall be killed. So I was thinking to grab the running processes with ps -e and do the filter via awk. Everything goes well until when we need to kill the processes. So my idea was to, while doing the search, add the PID of each process to an array. Then further after approval of the user, we would loop over the array and kill each process by its PID. – PHP May 15 at 17:08
  • 1
    Please don't put your reply here in the comments - it's all too easy to get lost. Please edit your question to include it there (ideally as if you'd included the information in the first place, and definitely not as "Edit" or "Update"). Make it easy to find all the information in one place and that makes it easy for someone to try and help you. – roaima May 15 at 17:36
  • Why use awk at all? You can easily do this processing directly in bash (or whichever shell you are using here) – nohillside May 16 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.