简体   繁体   中英

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.
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

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

You can use a temporary file and GNU time:

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.

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