[英]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. 我尝试完成一个shell脚本,但是我不知道如何完成最后一步,这可能是最简单的步骤。
That is attaching value to variable from find command. 那就是将值附加到find命令的变量中。
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. 但是现在,问题在于该文件可能未位于该文件中,因此我必须将其分配为find的结果。
How? 怎么样?
If your find
invocation outputs a single file along the lines of what you have shown, command substitution should do the trick 如果您的find
调用按照显示的内容输出单个文件,则命令替换应该可以解决问题
export PATH_TO_TEST_TDF_FILE="$(find . -type f -iname '*test.tdf*')"
Or, as BroSlow points out, 或者,正如BroSlow指出的那样,
export PATH_TO_TEST_TDF_FILE="$(find . -type f -iname '*test.tdf*' -print -quit)"
to have find
quit after the first file 在第一个文件之后find
退出
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. 将是PATH_TO_TEST_TDF_FILE =“ $(find -type f -iname test.tdf )”,但由于大多数情况下find返回一个以上文件,因此可能效果不佳。
Pro tip: The results of find should be assumed to not fit in a variable until proven otherwise. 专家提示:除非另外证明,否则应该假定查找结果不适合变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.