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

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".

What I actually wanted to ask for is the desired content of the variable, in a substantial perspective.

OK, you have a PID, that's good.

The posted "awk" would filter field # 4 of lsof's output against a variable whose name in the shell environment is "fileType" (setting it to "user" in the awk environment is not a problem).

Where in the script is this variable filled, and should it really be used as a filter against field # 4 which is "FD" (File Descriptor)?
FD can sometimes well be "mem" (yet followed by two more characters) as in the posted command in comment #39949872.

Anyway, I can't see why you should use arrays here, so there is no need for "eval" either:

Note: Use double plus single quotes around "'$fileType'"!
Here it's shown with spaces for illustration, but don't use the spaces: " ' $fileType ' "

Once the above has run you will find a file "/path/to/logfile.txt" which you can analyze whatever way.

Try both statements without redirection first ( omit ">> $logFile") so you can check the outcome on your terminal.
Reason: "mem" in the FD (field #4) means "memory-mapped file" and there might well be no such files held open by the process in question - so the "awk" output will be empty.

Featured Post

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Background
Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc...
As a "Unix" shop, our only readily …

Learn several ways to interact with files and get file information from the bash shell.
ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…