0

I'm trying to concatenate several commands to give a file path.

I need to run this:

ls -l /proc/{PID}/fd/{FILE_NAME}

I can get the PID by running:

lsof | grep 'mysql\(.*\)deleted' | awk { print $2 }'

And I can get the file name by running:

lsof | grep 'mysql\(.*\)deleted' | awk { print $10 } | cut -d'/' -f3

Both of those give me nice long lists, but how can I use them to generate the file path for the ls command?

5
  • Please show samples of the lists. – choroba Jan 29 '16 at 14:02
  • I have no way right now to copy them off the server anywhere but first is a line separated list of PIDs and second is a line seperated list of file names – Chris Jan 29 '16 at 14:05
  • Can the file names contain spaces? – choroba Jan 29 '16 at 14:05
  • the file names do not contain spaces no. – Chris Jan 29 '16 at 14:06
  • Try running lsof | grep 'deleted' and you'll see what I mean. – Chris Jan 29 '16 at 14:07
3

In bash, you can use arrays:

#!/bin/bash
pids=( $(lsof | grep 'mysql.*deleted' | awk '{ print $2 }') )
files=( $(lsof | grep 'mysql.*deleted' | awk '{ print $10 }' | cut -d/ -f3) )
for ((i=0; i<${#pids[@]}; i++)) ; do
    ls -l /proc/${pids[i]/fd/${files[i]}
done

Another possibility would be to read from two streams:

while read pid ; do
    read file <&3
    ls -l /proc/$pid/fd$file
done < <(lsof | grep 'mysql.*deleted' | awk '{ print $2 }') \
     3< <(lsof | grep 'mysql.*deleted' | awk '{ print $10 }' | cut -d'/' -f3)
1
  • @Chris: Check the update. – choroba Jan 29 '16 at 15:35

Your Answer

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

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