2

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?

2 Answers 2

6

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

Sign up to request clarification or add additional context in comments.

1 Comment

@1_CR You probably want to add -print -quit here, so it stops after finding one since behavior doesn't make sense otherwise.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.