运用awk if语句 函数调用 case语句 死循环。
1.先创建个用户管理文件(info.txt)
me passwd money ws 123 30000 qq 456 30000
2.创建银行系统(bank.sh)
#!/bin/bash menu1="=============== = 1.登录 = = 2.退出 = =============== 请选择您的操作[1|2]: " menu2="++++++++++++++++ +++ 1.查询 +++ +++ 2.存款 +++ +++ 3.取款 +++ +++ 4.转账 +++ +++ 5.退出 +++ ++++++++++++++++ 请选择您的操作[1|2|3|4|5]: " login(){ read -p "输入您的帐号: " account read -p "输入您的密码: " password passwd=`awk -v account=$account ‘{if($1==account)print $2}‘ ./info.txt` if [ -z "$passwd" ] 查看密码是否为空. then echo "帐号密码错误" elif [ $password == $passwd ] then echo "登录中....稍后....." sleep 1 echo "登录成功" sleep 1 clear else echo "帐号密码错误" fi } query(){ money=`awk -v account=$account ‘{if($1==account)print $3}‘ ./info.txt` echo "当前账户余额: $money" } storemoney(){ read -p "请输入存入金额: " var money=`awk -v account=$account ‘{if($1==account)print $3}‘ ./info.txt` all_money=$(($money+$var)) sed -i "/$account/s/$money/$all_money/" ./info.txt query } getmoney(){ read -p "请输入存入金额: " var money=`awk -v account=$account ‘{if($1==account)print $3}‘ ./info.txt` all_money=$(($money-$var)) sed -i "/$account/s/$money/$all_money/" ./info.txt query } forwardmoney(){ read -p "输入转账帐号: " new_name if awk -v account=$new_name ‘{if($1==new_name)print $1}‘ ./info.txt&>/dev/null then read -p "输入转账金额: " for_money money1=`awk -v account=$account ‘{if($1==account)print $3}‘ ./info.txt` if [ $money1 -lt $for_money ] then echo "余额不足!" else all_money1=$(($money1-$for_money)) sed -i "/$account/s/$money1/$all_money1/" ./info.txt money2=`awk -v account=$new_name ‘{if($1==account)print $3}‘ ./info.txt` all_money2=$(($money2+$for_money)) sed -i "/$new_name/s/$money2/$all_money2/" ./info.txt echo "转账成功" query fi else echo "转账帐号不存在!请确认" fi } while true do clear read -p "$menu1" num1 if [ $num1 -eq 1 ] then login while true do read -p "$menu2" num2 case $num2 in 1) query ;; 2) storemoney ;; 3) getmoney ;; 4) forwardmoney ;; 5) break ;; *) echo "请选择正确编号" ;; esac done elif [ $num1 -eq 2 ] then exit else echo "选择正确的编号" fi done
登陆成功之后进入当前界面
原文:https://www.cnblogs.com/MR-ws/p/11110978.html