-1

Trying to compare the process list before and after running a bash script of tests. Having trouble, since ps returns 1, and I'm not sure how to compare the before and after when I have them.

Ideally, it would look something like this. Forgive the crude pseudo-code:

run-tests: 
    ps -x
    export before=$?
    # run tests and scripts
    ps -x 
    export after=$?
    # compare before and after

Suggests and advice appreciated.

  • Pipe the output of ps through awk to return an exit code. – Raman Sailopal Jul 24 '17 at 15:10
  • When you say compare them, do you mean test whether they're equal, or perform some other automatic analysis, or put them both up on the screen, or what? – Beta Jul 24 '17 at 16:45

1 Answers1

1

I'm assuming you want to count the number of running processes before and after (your question wasn't overly clear on that). If so, you can pipe ps into wc:

export before=`ps --no-headers | wc -l`

-- EDIT ---

I reread the question, and it may be that you're looking for the actual processes that differ. If that's the case, then, you can capture the output in variables and compare those:

target:
    @ before=$$(ps --no-headers); \
      run test; \
      after=$$(ps --no-headers); \
      echo "differing processes:"; \
      comm -3 <(echo "$before") <(echo "$after")

A few quick notes on this: I concatenated all the lines using \'s as you mentioned you used makefiles, and the scope of a variable is only the recipe line in which it's defined. By concatenating the lines, the variables have a scope of the whole recipe.

I used double $$ as your original post suggested a makefile, and a makefile $$ will expand to a single $ in the bash code to be run.

Doing var=$(command) in bash assigns var the output of command

I used the <() convention which is specific to bash. This lets you treat the output of a command as file, without having to actually create a file. Notice that I put quotes around the variables -- this is required, otherwise the bash will ignore newlines when expanding the variable.

blackghost
  • 1,730
  • 11
  • 24