Export result from FIND command to variable value in linux shell script?
Solution 1
If your find
invocation outputs a single file along the lines of what you have shown, command substitution should do the trick
export PATH_TO_TEST_TDF_FILE="$(find . -type f -iname '*test.tdf*')"
Or, as BroSlow points out,
export PATH_TO_TEST_TDF_FILE="$(find . -type f -iname '*test.tdf*' -print -quit)"
to have find
quit after the first file
Solution 2
Would be PATH_TO_TEST_TDF_FILE="$(find -type f -iname test.tdf)" but probably doesn't work too well as find returns more than one file most of the time.
Pro tip: The results of find should be assumed to not fit in a variable until proven otherwise.
black_hat_cat
Updated on July 26, 2022Comments
-
black_hat_cat almost 2 years
I try to complete one shell script, but I don't have idea how to do final, and probably easiest step.
That is attaching value to variable from find command.
For example, if I execute:
find -type f -iname *test.tdf*
I will get output in example:
/root/Desktop/test.tdf
Now, I need a way to attach that value to for example:
export PATH_TO_TEST_TDF_FILE=/root/Desktop/test.tdf
But now, problem is that file may not be located there, so I must assign it to result from find.
How?