|
如何將字符型 2.98 轉換成數值型的 2.98 然後再進行數值計算。
小弟想寫個網絡測試腳本,需要把字符型轉成數值。。現在是無法計算。。。
ping -c 10 $1 >/tmp/nettest.temp
#extract and anlyze the network infomation from packet
local result1=`cat /tmp/nettest.temp|gawk '/packets/{print $6}'|sed s/%//`
local time_tmp=`cat /tmp/nettest.temp|gawk '/time/{print $7}'`
local time1=`echo $time_tmp|gawk '/time/{print $2}'|sed s/time=//`
local time2=`echo $time_tmp|gawk '/time/{print $5}'|sed s/time=//`
local time3=`echo $time_tmp|gawk '/time/{print $7}'|sed s/time=//`
local time4=`echo $time_tmp|gawk '/time/{print $9}'|sed s/time=//`
let time_tmp2=($time1+$time2+$time3+$time4)
local time_tmp2=$(($time_tmp2/4))
全碼如下:
#!/bin/bash
#This scripts writed by bill.jie
# if you have any question ,or find some bugs .Please email billjie1@gmail.com ,Thank You!
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin
#define the variable for server ip address
server1_ip=192.168.130.3
svr2_ip=192.168.130.8
svr3_ip=192.168.130.2
svr4_ip=192.168.130.4
svr5_ip=192.168.130.9
svr6ip=192.168.130.6
mail_ip=192.168.129.2
svr7_ip=172.16.3.254
svr9_ip=172.16.3.1
svr10_ip=10.0.0.254
svr11_ip=192.168.1.2
svr12=172.16.2.254
svr15_ip=192.168.0.18
#capture local host`s ip address
localhostip=`ifconfig eth0|gawk '/addr:/{print $2}'|sed s/addr://`
#argument inspecting
if [ -z "$localhostip" ]
then
echo "THE local host ip address is null.at `date`">>/var/log/erro_test.log
#teminal scripts
exit 2
fi
#test the tcp/ip protocol
lohoresult=`ping -c 4 $localhostip|gawk '/packets/{print $6}'|sed s/%//`
#if the local host test failred ,the scripts will terminaled
if [[ ($lohoresult -eq 100)||($lohoresult -gt 60) ]]
then
echo "tcp/ip test failre.at `date`">>/var/log/erro_test.log
#terminate scripte
exit 3
fi
#Next ,check the gateway
#capture gateway ip address
local_gateway_ip=`route |gawk '/default/{print $2}'`
#At first, check the gatway ip address
if [ -z "$local_gateway_ip" ]
then
echo "the gateway address is null ,script terminaled.at `date`">>/var/log/erro_test.log
kill -9 $$
fi
#test gateway
gtresult=`ping -c 10 $local_gateway_ip|gawk '/packets/{print $6}'|sed s/%//`
#scripts will be run as network quality
case $gtresult in
100)
echo "There is not a way to achieve gateway,scripts will be terminal. at `date`">>/var/log/erro_test.log
kill -9 $$
;;
50)
echo "The network worked poorly ,The scripts test will be not correct .at `date`">>/var/log/erro_test.log
;;
80)
echo "The network cannot able to test as poorly quality. at `date`">>/var/log/erro_test.log
kill -9 $$
;;
esac
#define a function to test server and output result
server_test()
{
if [ -z "$1" ]
then
echo "The argument is null,functions terminaled"
exit 5
fi
#capture network packet.
ping -c 10 $1 >/tmp/nettest.temp
#extract and anlyze the network infomation from packet
local result1=`cat /tmp/nettest.temp|gawk '/packets/{print $6}'|sed s/%//`
local time_tmp=`cat /tmp/nettest.temp|gawk '/time/{print $7}'`
local time1=`echo $time_tmp|gawk '/time/{print $2}'|sed s/time=//`
local time2=`echo $time_tmp|gawk '/time/{print $5}'|sed s/time=//`
local time3=`echo $time_tmp|gawk '/time/{print $7}'|sed s/time=//`
local time4=`echo $time_tmp|gawk '/time/{print $9}'|sed s/time=//`
let time_tmp2=($time1+$time2+$time3+$time4)
local time_tmp2=$(($time_tmp2/4))
#compare just test result with old infomation
if [[ ($result1 -eq 0)||($result1 -lt 70) ]]
then
if [ $time_tmp2 -lt 70 ]
then
echo "The $1 is UPline . quality : Good Average delay : $time_tmp2 ">>/tmp/compare.tmp
else
if [ $time_tmp2 -lt 100 ]
then
echo "The $1 is OK . But quality : badly Average delay : $time_tmp2">>/tmp/compare.tmp
else
echo " The $1 is Ok . But quality : poorly Average delay : $time_tmp2">>/tmp/compare.tmp
fi
fi
else
case "$1" in
"$cnrouter_ip")
echo "Emergency,The cn router down off ">>/tmp/compare.tmp
;;
"$twvpndev")
echo "Emergency, vpn network with taiwan interrupted ">>/tmp/compare.tmp
;;
"$cnvpndev_ip")
echo "Emergency,cn vpn device down off ">>/tmp/compare.tmp
;;
"$firwall_ip")
echo "Emergency,firewall maybe down off ">>/tmp/compare.tmp
;;
"$twmail_ip")
echo "waring , taitwan mail server may be down off " >> /tmp/compare.tmp
;;
*)
echo "The $1 is Downline. quality : zero Average delay : null ">>/tmp/compare.tmp
;;
esac
fi
} |
|