Difference between revisions of "bash arithmetic"

From thelinuxwiki
Jump to: navigation, search
 
Line 22: Line 22:
  
 
  a=`expr 5 + 3`
 
  a=`expr 5 + 3`
 +
 +
== bash built in hex 2 dec ==
 +
'''hex digits ab a1 48 39'''
 +
 +
$ echo $((0xab))
 +
171
 +
$ echo -e $((0xab)) $((0xa1)) $((0x48)) $((0x39))
 +
171 161 72 57
 +
 +
'''hex to dotted quad IP notation'''
 +
 +
$ echo 'aba14839' |sed 's/../ 0x&/g' |xargs printf "%d.%d.%d.%d\n"
 +
171.161.72.57
 +
 
 +
  
 
[[category:bash]]
 
[[category:bash]]

Latest revision as of 02:17, 2 January 2015

using let

let "TOTAL = ( $A + 5 ) / $b"


floating point using bc

TOTAL=`echo "( $A + 5 ) / $b" | bc -l

using scale to control number of digits

$ echo "1 / 100" | bc -l 
.01000000000000000000

$ echo "scale =2; 1 / 100" | bc -l                                                                        ~
.01


using expr

a=`expr $a + 1`
a=`expr 5 + 3`

bash built in hex 2 dec

hex digits ab a1 48 39

$ echo $((0xab))
171
$ echo -e $((0xab)) $((0xa1)) $((0x48)) $((0x39))
171 161 72 57

hex to dotted quad IP notation

$ echo 'aba14839' |sed 's/../ 0x&/g' |xargs printf "%d.%d.%d.%d\n"
171.161.72.57