How can I calculate time elapsed in a Bash script?

Bash has a handy SECONDS builtin variable that tracks the number of seconds that have passed since the shell was started. This variable retains its properties when assigned to, and the value returned after the assignment is the number of seconds since the assignment plus the assigned value. Thus, you can just set SECONDS to … Read more

How to find/replace and increment a matched number with sed/awk?

I think finding file isn’t the difficult part for you. I therefore just go to the point, to do the +1 calculation. If you have gnu sed, it could be done in this way: sed -r ‘s/(.*)(\?cache_version=)([0-9]+)(.*)/echo “\1\2$((\3+1))\4″/ge’ file let’s take an example: kent$ cat test ello barbaz?cache_version=3fooooo bye kent$ sed -r ‘s/(.*)(\?cache_version=)([0-9]+)(.*)/echo “\1\2$((\3+1))\4″/ge’ test … Read more

Divide two variables in bash

shell parsing is useful only for integer division: var1=8 var2=4 echo $((var1 / var2)) output: 2 instead your example: var1=3 var2=4 echo $((var1 / var2)) ouput: 0 it’s better to use bc: echo “scale=2 ; $var1 / $var2” | bc output: .75 scale is the precision required