#!/bin/bash

# current time:2016-11-04 10:40:14

#数据库信息

prot="3306"

hostname="localhost"

username="root"

password="123456"

DB_name="Bank"

table_name="bank"

cmd="mysql -u${username} -h${hostname} -p${password} -e"

#验证登录

Verification() {

    read -p "输入你的用户名: " user

    read -s -p "输入密码: " pass

    echo

    echo "正在验证请稍候......"

}

Verification

while true

do

  if [ `$cmd "select * from ${DB_name}.${table_name} where name='${user}' and pass='${pass}'"|wc -l` -ne 0 ];then

     sleep 3

     echo "验证成功"

     break

  else

     sleep 3

     echo "用户名或密码错误"

     Verification

  fi

done

#查询

query() {

       money=`$cmd "select money from ${DB_name}.${table_name} where name='${user}' and pass=${pass}" | grep -v "money"`

}

#存款

deposit() {

       read -p "输入存款金额:" new_money

       let total=${money}+${new_money}

       $cmd "update ${DB_name}.${table_name} set money='${total}' where name='${user}' and pass=${pass}"

}

#取款

remove_money() {

       query

       read -p "输入取款金额:" remove

       if [ $remove -gt $money ];then

          echo "余额不足,可用余额$money"

       else

          let get=$money-$remove

          $cmd "update ${DB_name}.${table_name} set money='${get}' where name='${user}' and pass=${pass}"

          echo "成功取款${remove},请拿好现金"

       fi

}

#修改密码

chage_pass() {

       read -s -p "输入密码:" new_pass

       echo

       read -s -p "请再次输入密码: " again_pass

       echo

       if [ "${new_pass}" != ${again_pass} ];then

          echo "两次输入的密码不一样"

       else

          $cmd "update ${DB_name}.${table_name} set pass='${new_pass}' where name='${user}' and pass=${pass}"

       fi

}

while true

do  

  read -p "(这里输入编号就可以)1:查询 2:存款 3:取款 4:修改密码 5:退出 " action

  case ${action} in

  1)

    query

    sleep 3

    echo "可以余额为${money}"

    ;;

  2)

    deposit

    sleep 3

    echo "成功存款${new_money}"

    ;;

  3)

    remove_money

    sleep 3

    ;;

  4)

    chage_pass

    sleep 3

    echo "操作成功"

    ;;

  5)

    echo "谢谢,请保管好个人财产"

    break

    ;;

  esac

done