简体   繁体   English

将命令的输出和执行时间保存在bash脚本中的不同变量中

[英]save output and execution time of command in diffrent variables in bash script

I want to save the output of the find command in $path variable, and save the execution time of this command in t variable. 我想将find命令的输出保存在$ path变量中,并将此命令的执行时间保存在t变量中。
something like this, but its incorrect. 像这样的东西,但是它是不正确的。

t=`time path=`find . -type d  -iname "$x"` `

the blow command works well, but this is in the loop and I want to have the sum of the time in a variable blow命令效果很好,但这在循环中,我想将时间总和包含在变量中

time path=`find . -type d  -iname "$x"`

You can use a temporary file and GNU time: 您可以使用一个临时文件和GNU时间:

TMPFILE="$(mktemp)"
path="$( /usr/bin/time -o "$TMPFILE" find . -type d  -iname "$x" )"
t="$(cat "$TMPFILE")"
rm -f "$TMPFILE"

For higher security, you can also use a temporary directory etc. 为了提高安全性,您还可以使用临时目录等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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