#!/bin/bash rate='1.2' # 1.2 CNY/Gb orig_rate='0.72' # Server Traffic: 0.72 CNY/g pay_req='40.80' # Server Fee: 40.80 CNY/mo unit='CNY' function report { total_get=$(echo "scale=5; $(stat | awk -F" " '{print $11}' | tr '\n' '+')0" | bc) total_pay=$(echo "($(cat paid.log usage.log | awk -F ":| " '{print $11}' | tr '\n' '+')0)/1024/1024/1024*$orig_rate+$pay_req"|bc) echo "Income : $total_get $unit" echo "Cost : $total_pay $unit" echo "Revenue : $(echo "scale=5; $total_get-$total_pay"|bc) $unit" } function stat { users=$1 [[ -z $1 ]] && users="$(cat usage.log | awk -F" " '{print $1}' | sort | uniq )" for user in $users do bytes=$(echo $(cat usage.log | grep $user | awk -F ":| " '{print $11}' | tr '\n' '+')0 | bc) kbytes=$(echo "scale=5; $bytes/1024" | bc) mbytes=$(echo "scale=5; $kbytes/1024" | bc) gbytes=$(echo "scale=5; $mbytes/1024" | bc) fee=$(echo "scale=5; $gbytes*$rate" | bc) echo "User $user, traffic $bytes bytes ($mbytes Mb, $gbytes Gb), fee $fee $unit." done } function pay { [[ -z $1 ]] && return 1 stat $1 read -p "User $1 paied? (yes/no)" yn [[ $yn == 'yes' ]] && { echo "Removed all records for $1. Backup can be found in paid.log and usage.log.bak." cat usage.log | grep $1 >> paid.log cp usage.log usage.log.bak cat usage.log.bak | grep -v $1 > usage.log } || echo "Aborting." } $*