Link to home
Start Free TrialLog in
Avatar of pvinodp
pvinodp

asked on

How to add lsof with pipe and awk to a array variable?

I have to populate a array which will be commands and I would run them in a loop to execute the commands:

for i in "${cmd_array[@]}"
        do
                ${i} >> $logFile
        done

Populating the command-array:

cmd_array=("cat /proc/${process_pid}/status")
cmd_array[1]="lsof -s -p $process_pid | awk '{ user = \$4 } { if ( user == $fileType ) { print } }' | sort -nrk 7 |  head -100"

I get the following error when i execute the cmd_array[1]:
lsof -s -p 5573 '|' awk ''\''{' user = '$4' '}' '{' if '(' user == mem ')' '{' print '}' '}'\''' '|' sort -nrk 7 '|' head -100

lsof: status error on |: No such file or directory
lsof: status error on awk: No such file or directory
lsof: status error on '{: No such file or directory
lsof: status error on user: No such file or directory
lsof: status error on =: No such file or directory
lsof: status error on $4: No such file or directory
lsof: status error on }: No such file or directory
lsof: status error on {: No such file or directory
lsof: status error on if: No such file or directory
lsof: status error on (: No such file or directory
lsof: status error on user: No such file or directory
lsof: status error on ==: No such file or directory
lsof: status error on mem: No such file or directory
lsof: status error on ): No such file or directory
lsof: status error on {: No such file or directory
lsof: status error on print: No such file or directory
lsof: status error on }: No such file or directory
lsof: status error on }': No such file or directory
lsof: status error on |: No such file or directory
lsof: status error on sort: No such file or directory
lsof: status error on -nrk: No such file or directory
lsof: status error on 7: No such file or directory
lsof: status error on |: No such file or directory
lsof: status error on head: No such file or directory
lsof: status error on -100: No such file or directory

How to process lsof commands this way?
SOLUTION
Avatar of gheist
gheist
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pvinodp
pvinodp

ASKER

i do not have a backslash at the pipe!..
What do u mean by escaping pipe command.

cmd_array[1]="lsof -s -p $process_pid | awk '{ user = \$4 } { if ( user == $fileType ) { print } }' | sort -nrk 7 |  head -100"

Above is my oroginal command.
But when i try to execute it by :
for i in "${cmd_array[@]}"
        do
                ${i} >> $logFile
        done

I get the following error:
lsof -s -p 5573 '|' awk ''\''{' user = '$4' '}' '{' if '(' user == mem ')' '{' print '}' '}'\''' '|' sort -nrk 7 '|' head -100
Avatar of woolmilkporc
Try

eval ${i} >> $logFile

And wouldn't the user appear in column 3, thus requiring "$3" instead of "$4" in awk?

Next, you're missing (escaped) double quotes here:

if ( user == \"$fileType\" )

Strings must be quoted in awk so that they can be distinguished from variables.


cmd_array[1]="lsof -s -p $process_pid | awk '{ user = \$3 } { if ( user == \"$fileType\" ) { print } }' | sort -nrk 7 |  head -100"

eval ${cmd_array[1]}


In your (and the above) version process_pid and fileType must be set before populating the array. Is this desired?
Avatar of pvinodp

ASKER

is there anything wrong or is it not the optmized way?
Once the suggested corrections have been made there's nothing wrong anymore. Using "eval" the command should run as desired (see my note about populating variables, however).

To be able to ponder what the "optimitzed" way might be I'll need much more info what you're actually trying to achieve, and in which context.

I don't understand what this

cmd_array=("cat /proc/${process_pid}/status")

should be good for.
The statement will fill ${cmd_array[0]} with all the stuff from the status part of the process entry in /proc, which is not at all related to the lsof command you posted. ${cmd_array[0]} will not contain a command, so running it will always produce errors, even with "eval".
Avatar of pvinodp

ASKER

I am doing a performance analysis of a particular process id .

i need to execute "cat /proc/pid/status" and "lsof ****" .
how can i proceed?
Do you want to run the analysis against a specific pid/user combo, or how should pid and user be selected?

What do you actually mean with "user" in your script? The username as displayed by lsof in column 3, or perhaps something different?
Avatar of pvinodp

ASKER

user is just a placeholder .. The variable can have any name.
i want to measure against particular pid
I am given the pid for whcih i have to measure.

The code to run the commands is comon. i have a array of commands for certain type of pids.
For one such pid there  is 2 commands:
1. cat /proc/pid/status
2. lsof ****

the actual problem is that i am unable to append the eval output to a file.
THat is my next question. Can someone answer with that perspective?
You know that lsof can output just couple of fields without header?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pvinodp

ASKER

I changed the command from using a variable to use a static constant.
if ( user ~"'$fileType'" )  is changed to if ( user ~"mem" )

what is the difference between == and ~
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pvinodp

ASKER

Thanks for your response