shell实现除法,保留小数点后N位

时间:2021-11-15 18:35:14
$more get_wstts_success.sh
#!/bin/bash

open_gw_1=$(ssh admin@open-gw-1 "sh /home/admin/bin/check_wstts_success.sh")
open_gw_2=$(ssh admin@open-gw-2 "sh /home/admin/bin/check_wstts_success.sh")
#echo $open_gw_1
#echo $open_gw_2
gw_1_all=$(echo $open_gw_1 | cut -d " " -f1)
gw_1_err=$(echo $open_gw_1 | cut -d " " -f2)
#echo $gw_1_all
#echo $gw_1_err
gw_2_all=$(echo $open_gw_2 | cut -d " " -f1)
gw_2_err=$(echo $open_gw_2 | cut -d " " -f2)
#echo $gw_2_all
#echo $gw_2_err

if [ $gw_1_all -eq 0 ] && [ $gw_2_all -eq 0 ]; then
    echo 1.00000
else
    successrate=$(echo "scale=5;rate=($gw_1_all + $gw_2_all - $gw_1_err - $gw_2_err) / ($gw_1_all + $gw_2_all);if(length(rate)==scale(rate)) print 0;print rate"|bc)
    echo $successrate
fi

 

第二种实现方法:

awk 'BEGIN{printf "%0.6f",5/6}'