I would like to know is there a way to take the script execution time
For e.g i am having a script.sh i need to write inside he script.sh like
Start time : 10-Mar-2016 02:30:35
all code over here
...
End time : 10-Mar-2016 03:30:32
Script start time - 02:30:35
End time - 03:30:32
Total time taken taken - 01 hour
I need to display in hours and mins, like 01 hour 30 mins etc
Code i have tried but this is in seconds i need to display like above
start_time=$(date +%s)
finish_time=$(date +%s)
echo "Total time taken taken: $((finish_time - start_time)) secs."
Thanks for your input rothbat, is there a way to achieve without using arithmetic functions or any other way to achieve just wanted to know in simpler way.
@Rudic Sorry i dint get your question , can you let me know whether its achievable with time command i mean to calculate the time difference in hours and mins
I'm not sure how you're going to get the elapsed time without arithmetic if you are starting with what you have... starting and ending times...
Rudic's suggesting of the time command is definitely another option as it's a command designed to report back how long some other command/script took to execute, but you may have to be creative about when/where you use it since it reports back how long a single step (which can be another script) took to execute.
For example the "sleep 4" command takes 4 seconds to execute, but if you issue "time sleep 4" it reports back more detailed info on the command.
$ time sleep 4
real 0m4.035s
user 0m0.000s
sys 0m0.008s
I'm not sure if it's useful for you to have the elapsed time displayed that way. You can set the format it uses by passing in a format string but frankly, if you ask me... dealing with the simple arithmetic is the most straight forward way to handle it.
If you have 2 steps inside a script that both take time, you can't call them separate with time and have it reflect one time, it'll simply reflect how long each step took individually.
I'm not sure if his requirement is rigid, but he did say he needed to reflect the elapsed time "inside" his script. If he misspoke, then time is what I'd use and your solution looks nice.