I have the following command

time compare -metric rmse -subimage-search -dissimilarity-threshold 1 -similarity-threshold 0.99 $page \( -size 1x1 xc:black \) null:

which produces the following results:

0 (0) @ 7,0
real    0m28.366s
user    0m25.400s
sys     0m1.500s

I'm looking to store the results of the command in an array, as such

declare -a Pageinfo=($(time compare -metric rmse -subimage-search -dissimilarity-threshold 1 -similarity-threshold 0.99 out1.tif \( -size 1x1 xc:black \) null: ))

echo "Pageinfo results  =  ${Pageinfo[*]}"

however, array is empty. Why?

  • @mikeserv won't work. You need a subshell and you must redirect the stderr of the subshell. time is really annoying that way. – terdon Nov 8 '14 at 17:23
  • It's not clear what your trying to achieve: what do you want to store in the array? the result of compare or the result of time? (or both?). Because as you wrote your command, Pageinfo contains what compare outputs on standard output. – gniourf_gniourf Nov 8 '14 at 17:49
  • acually, want both results, that of the compare and time command in the array. – user68650 Nov 8 '14 at 19:03
  • Please. clarify exactly what you want. Saying actually, want both results doesn't tell us how you want them. If you want the result of time in an array myTime and the result of the command in an array Pageinfo, please make that very, very clear in your question. (No offense, but you're not enough clear and precise in your question). – gniourf_gniourf Nov 8 '14 at 23:54
  • Forgive me I was not clear enough, and no offense taken. I would like to store the results in one array, starting with the results of the convert command, follow be the results of time. so ideally, the array should have contain the following. -- 0 (0) @ 7,0 real 0m28.366s user 0m25.400s sys 0m1.500s -- I have been trying to do so, but not there yet. and thank you the guidance. To see the acutall example will benefit greatly! – user68650 Nov 9 '14 at 2:07

The time command will print its output to standard error, not standard output. So that's what you need to capture. Then, you need to capture the output of time and not the output of the command you are timing. Typically, this is done by grouping or running the commands in a subshell (in { } or ( ) respecitvely), redirecting the group's output to /dev/null and redirecting its error to standard output. For example:

$ declare -a array=( $( { time ls; } 2>&1 >/dev/null ))
$ echo ${array[*]} 
real 0m0.003s user 0m0.000s sys 0m0.000s

To save each line as an array element, use the mapfile builtin. However, the array created will only be available within the grouping:

$ { time ls; } 2>&1 >/dev/null | { 
    mapfile  array1; 
    echo "1:     ${array1[1]}2:${array1[2]}3:${array1[3]}"; 
1: real 0m0.003s
2:user  0m0.000s
3:sys   0m0.000s

I'm not sure if you want the output of the command in the array or only the output of time. If you want the command as well, remove the > /dev/null from each of the examples above.

| improve this answer | |
  • Now if you also need to have the output of the command on the terminal, you'll need to do some plumbing ;). – gniourf_gniourf Nov 8 '14 at 17:34
  • @gniourf_gniourf true, grouping also works. I'd used it for the mapfile approach but used the subshell for the other out of habit. Answer edited. – terdon Nov 8 '14 at 17:44
  • 1
    @mikeserv you mean time? It's either a shell keyword or a separate binary at /usr/bin/time. I used command as a generic term. – terdon Nov 8 '14 at 17:45
  • True - I completely forgot about the separate executable. But about times I meant times. – mikeserv Nov 8 '14 at 17:52
  • @user68650 If this answer solves your issue please take a minute and click the check mark under the vote count to the left, this will signify to everyone that your issue's been resolved and is the way that thanks are expressed in the site. – terdon Nov 8 '14 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.