Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # timelast - shows how long the last running command lasted
- # http://idnotfound.wordpress.com/2008/10/14/a-bash-plugin-timelast/
- #
- # this is the command called by the user
- timelast () {
- # shows a pretty output like '1h23m45s'
- # how many seconds it took
- ret="$(( $_timelast_time % 60 ))s"
- cur=$(( $_timelast_time / 60 ))
- # tests for minutes
- if [ $cur -gt 0 ]; then
- ret="$(( $cur % 60 ))m${ret}"
- cur=$(( $cur / 60 ))
- fi
- # tests for hours
- if [ $cur -gt 0 ]; then
- ret="${cur}h${ret}"
- fi
- # echoes on the screen
- echo $ret
- return 0
- }
- # internal command that calculates the running time of the last process
- # (add to $PROMPT_COMMAND, so it is executed at each prompt -- see below)
- _timelast_promptcommand () {
- # grab the timestamp of the last command from history
- # (depends on $HISTTIMEFORMAT="%s " -- see below)
- _timelast_start="$(history | tail -n1 | awk '{ print $2 }')"
- # now grab the timestamp for the current time
- _timelast_end="$(date +%s)"
- # subtract and... voila! elapsed seconds! :-)
- _timelast_time="$(( $_timelast_end - $_timelast_start ))"
- return 0
- }
- ## sets up initial values and important variables in bash
- # timestamps the command history so we know when commands were performed
- export HISTTIMEFORMAT="%s "
- # calculates how long the last command ran for at every prompt shown
- export PROMPT_COMMAND="_timelast_promptcommand; ${PROMPT_COMMAND}"
Advertisement
Add Comment
Please, Sign In to add comment