Placing arbitrary content before exiting bash time builtin

I have the following bash - script:

#!/bin/bash

printColored(){
  echo -e "\e[0;"$2"m \t"$1" \e[0;37m"
}

printColored "Executing query with usual tables." 34
time hive -f query1.sql
printColored "Executing query with RCFile tables." 34
time hive -f query2.sql

      

It gives me the following output:

    Executing query with usual tables. 
...
a lot of output of hive command
...
real    1m8.928s
user    0m36.283s
sys 0m2.157s
    Executing query with RCFile tables. 
...
hive output again
...
real    1m9.376s
user    0m37.186s
sys 0m2.168s

      

How can I change my script if I need all the hive

output before echo

and time after it?
That is, the output should be in the following order:

...hive 1...
...hive 2...
    Executing query with usual tables. 
real    1m8.928s
user    0m36.283s
sys 0m2.157s
    Executing query with RCFile tables. 
real    1m9.376s
user    0m37.186s
sys 0m2.168s

      

If this is not possible, how can I remove the output of the hive?

+3


source to share


2 answers


The output of a bash command time

can be controlled by a shell variable TIMEFORMAT

. To enable the desired messages with normal time information:



TIMEFORMAT=$'\nExecuting query with usual tables\nreal\t%3lR\nuser\t%3lU\nsys%3lS'
time hive -f query1.sql
TIMEFORMAT=$'\nExecuting query with RCFile tables\nreal\t%3lR\nuser\t%3lU\nsys%3lS'
time hive -f query2.sql
unset TIMEFORMAT

      

+2


source


I believe time is outputting to a std error, so you can try redirecting that to a temp file and then displaying at the end - something like



tmpfile=`mktemp`
(time hive -f query1.sql) 2>  $tmpfile
(time hive -f query2.sql) 2>> $tmpfile
cat $tmpfile
rm $tmpfile

      

+1


source







All Articles