Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Putting arbitrary content before output of bash time builtin

Tags:

bash

shell

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 hive output before echo and time output after it? I.e. output must 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 impossible, how can I eliminate hive output?

like image 346
VeLKerr Avatar asked Nov 23 '25 04:11

VeLKerr


1 Answers

The output of bash's time command can be controlled with the TIMEFORMAT shell variable. To include your desired messages with the usual 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
like image 109
John1024 Avatar answered Nov 24 '25 18:11

John1024