shell脚本小数运算
1.awk
hadoop@Mcnode1:~/cloud/adam/xubo/data/test20160310/test$ cat demical.sh
#!/bin/bash
echo awk -v x=2.45 -v y=3.123 'BEGIN{printf "%.2f\n",x*y}'
s=1.5
echo
s
e
c
h
o
‘
a
w
k
−
v
x
=
2.45
−
v
y
=
"
s echo `awk -v x=2.45 -v y="
secho‘awk−vx=2.45−vy="s" ‘BEGIN{printf “%.2f\n”,x*y}’`
结果:
hadoop@Mcnode1:~/cloud/adam/xubo/data/test20160310/test$ ./demical.sh
7.65
1.5
3.68
可以从外部获取变量,也可以自定义变量
2.|bc
hadoop@Mcnode1:~/cloud/adam/xubo/data/test20160310/test$ cat demical.sh
#!/bin/bash
echo awk -v x=2.45 -v y=3.123 'BEGIN{printf "%.2f\n",x*y}'
s=1.5
echo
s
e
c
h
o
‘
a
w
k
−
v
x
=
2.45
−
v
y
=
"
s echo `awk -v x=2.45 -v y="
secho‘awk−vx=2.45−vy="s" ‘BEGIN{printf “%.2f\n”,xy}'#product=
awk -v x=2.45 -v y="$s" 'BEGIN{printf “%.2f\n”,xy}’`
s2=$(echo “scale=3; 6 / 5” | bc)
echo $s2
s3=$(echo “scale=3; $s / 5” | bc)
echo
s
3
结
果
:
h
a
d
o
o
p
@
M
c
n
o
d
e
1
:
/
c
l
o
u
d
/
a
d
a
m
/
x
u
b
o
/
d
a
t
a
/
t
e
s
t
20160310
/
t
e
s
t
s3 结果: hadoop@Mcnode1:~/cloud/adam/xubo/data/test20160310/test
s3结果:hadoop@Mcnode1: /cloud/adam/xubo/data/test20160310/test ./demical.sh
7.65
1.5
3.68
1.200
.300