Archive for category Shell Scripting

Returning linux timestamp

To return linux timestamp use the following:

Date and time now in timestamp format:
date "+%s"

A specific date and time in timestamp format:

date -d "<time>" "+%s"
e.g. date <del>d "2014</del>01-30 15:00:00" "+%s"
</time>

Date and time now in timestamp format and nano seconds:

date "+%s%N"

Date and time now in timestamp format and milli seconds:

date “+%s%N” | cut -b1-13
or
echo $(($(date “+%s%N”)/1000000))

To return a specific date and time in timestamp with nano or milli seconds then simply add -d parameter similar to the previous example.

No Comments

How to convert Scientific Notation number to Decimal using bash?

Turns out this is pretty easy to do using awk and printf in bash!

Lets say you have a scientific value of “2.1061863333e+03” and you wish to convert it to decimal. You can do something like below:

>> echo "2.1061863333e+03" | awk {' printf "%12.2fn",$1 '}
     2106.19

  • Obviously you can do the same if you have series of values in a file using cat.
  • I found this specially useful when obtaining values from RRDs.

No Comments