Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I want to collect total RSS used by all foo processes and pass it to quux script as an argument.

function collect_foo {

        local X=0
        local P
        local M
        for P in $(pgrep foo)
        do
                M=$(cat /proc/$P/statm | awk '{ print ($2 * 4096) }')
                X=$[X+M]
        done
        echo $X
}

quux $(collect_foo)

cat can accept multiple arguments. Is it possible to construct all /proc/$P/statm paths without an explicit loop? For example, by using pgrep -d , foo I can make a comma-separated PID list suitable for {}. Then the output can be passed to awk that sums it up using BEGIN/END blocks.

I'm stuck at {}. Can something like cat /proc/{$(pgrep -d , foo)}/statm be done? {} doesn't expand.

I know that in general summing up RSS is not a good idea, the question is about indirect shell expansion. Any other means of shorten the code are welcome though.

share|improve this question

closed as off-topic by 200_success Jun 4 '14 at 10:19

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions must involve real code that you own or maintain. Questions seeking an explanation of someone else's code are off-topic. Pseudocode, hypothetical code, or stub code should be replaced by a concrete example." – 200_success
If this question can be reworded to fit the rules in the help center, please edit the question.

    
The code I ended with is quux $(eval cat /proc/{$(pgrep -d , foo)}/statm | awk '{sum += $2}END{ print (sum * 4096)}') –  nponeccop Jun 4 '14 at 8:52

1 Answer 1

I'm not sure this question belongs to Code Review. Nevertheless, brace expansion happens too early, but you can use eval to evaluate it again:

eval cat /proc/{$(pgrep -d . foo)}/statm
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.