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 program

i 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

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -