Wednesday, April 13, 2022

[SOLVED] Linux find, get output to a variable before and after -exec respectively

Issue

In a shell script (sh) I want to find an executable file, retain its run output in a variable and its path in another variable.

Say I want to find all installed OpenCV versions, then retain the major in a variable and the location of the file in another variable.

list=$(find / -type f -name opencv_version -exec {} \; 2>/dev/null | sort -u | cut -c-1); printf "%c;" $list

will return say 3;4; but I also want the opencv_version file location for version 3 and 4 respectively. For that I would need to store the output of the first part find / -type f -name opencv_version in another variable, before running -exec {} \;

Can I do these two ops in a one liner?


Solution

I guess I don't need -exec afterall. This works for me:

for i in $(find / -type f -name opencv_version 2>/dev/null); do printf '%c in %s\n' $($i) $i; done

This will return something like:

4 in /usr/bin/opencv_version

3 in /home/user/opencv/build/bin/opencv_version



Answered By - Dani Ursu
Answer Checked By - Mildred Charles (WPSolving Admin)