I'm looking for a way to do a simple math calc during a shell script as a means of logging how long a particular task takes.
For example...
STARTTIME=whenever this script starts
./path/to/command.sh >>logfile.log
TOTALTIME=<time at this stage of the script after above command finished> - $STARTTIME
ECHO This command took $TOTALTIME to complete. >>logfile.log
I know that's a crude representation, but hopefully enough to communicate what I'm looking to accomplish.
Variables don't work that way. You'd want to put it in `backticks` to put in in a variable.
Also, you don't want spaces after the equals sign. VARIABLE=something is good, VARIABLE=" something" is good, VARIABLE= something does something very different -- it blanks VARIABLE before attempting to run the program something...
But why not just jam it right into the printf statement, get it all done in one go?
printf "This script took %02dH %02dM %02dS to run\n" \
$(( SECONDS/(60*60) )) $(( SECONDS/60 )) $(( SECONDS%60 ))
The \ on the end of the line lets you continue it on the next line, instead of making one super-long line...
No problem. It's hard to know what to ask until you know at least a little... Check out the reference cards of the Advanced Bash Scripting Guide, they clear up some mysteries.