linux - Bash counting executed time -
i want write script in bash save file how long have been executed
i want output this: 1 minute 2 minute ...
requirements
you need install time
(not shell built-in).
to validate have right one: $ time
this expected output:
$ time /usr/bin/time
solution
assuming have function called main
, in main scripting code included
function main() { echo "sleeping .." sleep(5) echo "this first arg: ${1}" echo "this second arg: ${2}" }
to time function call, follows (arguments reference):
main "hello" "world" | $(which time) -o "output_filename_for_time" -f "%e" $(which bash)
explanation
we piping /usr/bin/time function call time it. calling time
using $(which time)
because not want shell built-in. after passing -o
argument define our output file , -f
argument used define time format. in example, i used seconds. in end, pass shell using, , in our case using bash, $(which bash)
.
man time
read other formats , of course proper usage of programi use seconds because easier convert them anything.
edit #1
you can use gnu tool command
instead of using absolute path of time
$ command time
instead of
$ $(which time)
Comments
Post a Comment