简体   繁体   中英

Export result from FIND command to variable value in linux shell script?

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM