shell实用技巧:文件统计信息和银行账户管理系统

简介: shell实用技巧:文件统计信息和银行账户管理系统

需求

       写一个shell程序命名为wc.sh,统计文件字符数、行数和单词数。

       写一个shell程命名为bank.sh,完成以下功能:退出、存款、取款、添加账户、删除账户、查询账户、显示所有账户,所有信息保存在account.dat文件中,每次操作会实时更新account.dat文件。

c版本

   

实用技巧:文件统计信息和银行账户管理系统》:

      https://blog.csdn.net/qq21497936/article/details/80026348

实现截图

wc.sh

       图片.png

bank.sh

图片.png图片.png


wc.sh脚本

#!/bin/sh
#获取参数个数
#记录参数个数,从参数1开始计算
argc=$#
function doCmd(){
  echo "===== file: $1 ====="
  m=`cat $1 | wc -m`
  l=`cat $1 | wc -l`
  w=`cat $1 | wc -w`
  echo "字符数: $m" 
  echo "换行数: $l"
  echo "单词数: $w"
}
#不带参数则手动输入一个文件名
if [ $argc -eq 0 ]
then
  read -p "input a file name: " input
  # 判断文件是否存在
  if [ ! -e $input ]
  then
    echo "$input is not found"
    # 不存在退出
    exit
  fi
  # 打印出文件信息
  doCmd $input
else
  # 参数传递了文件名字
  for par in $@; do
    # 打印出文件名
    # echo $par
    # 判断文件是否存在
    if [ ! -e $par ]
    then
      echo "$par is not found"
      # 不存在则退出
      exit
    fi
    #存在,则开始统计数量
    doCmd $par
  done
fi
exit


bank.sh脚本

#!/bin/bash
# 存储的数据文件
file=accounts.dat
function showMenu()
{
  echo "========== 银行账户菜单 =========="
  echo "0.退出"
  echo "1.存款"
  echo "2.取款"
  echo "3.添加账户"
  echo "4.删除账户"
  echo "5.查询账户余额"
  echo "6.打印所有账户信息"
  echo "================================="
  read -p "请输入您的选择(0-6):" number
}
function menu1()
{
  read -p "请输入存款账户:" account
  read -p "请输入存款金额:" money
  # 检查是否有该账户
  result=`sed -n "/:$account:/p" $file`
  if [ "$result" = "" ]
  then
    echo "未查找到该账户!!!"
  else
    balance=`echo $result | awk -F ':' '{ print $5 }'`
    total=`echo "scale=2;$balance+$money" | bc`
    # 替换
    sed -i "s/:$account:$balance/:$account:$total/" $file
    echo "存款成功!!!"
  fi
}
function menu2()
{
  read -p "请输入取款账户:" account
  read -p "请输入取款金额:" money
  # 检查是否有该账户
  result=`sed -n "/:$account:/p" $file`
  if [ "$result" = "" ]
  then
    echo "未查找到该账户!!!"
  else
    balance=`echo $result | awk -F ':' '{ print $5 }'`
    total=`echo "scale=2;$balance-$money" | bc`
    # 判读是否为大于等于0
    if [ $total -ge 0 ]
    then
      # 替换
      sed -i "s/:$account:$balance/:$account:$total/" $file
      echo "取款成功!!!"
    else
      echo "取款余额不足!!!"
    fi
  fi
}
function menu3()
{
  read -p "请输入 Fisrt name :" firstName
  read -p "请输入 Middle name :" middleName
  read -p "请输入 Last name :" lastName
  read -p "请输入 新帐号 :" account
  # 检查该账户是否是6位
  if [ `expr length $account` != 6 ]
  then
    echo "输入账户必须为6位整数!!!"
    return
  fi
  # 检查该账户是否是整数
  expr $account + 0 > /dev/null
  if [ $? -ne 0 ]
  then
    echo "输入账户必须为6位整数11!!!"
    return
  fi
  # 检查是否有该账户account
  result=`sed -n "/:$account:/p" $file`
  if [ "$result" != "" ]
  then
    echo "账户$account已存在!!!"
    return
  fi
  # 检查是否有该用户信息
  result=`sed -n "/$firstName:$middleName:$lastName:/p" $file`
  if [ "$result" = "" ]
  then
    echo "$firstName:$middleName:$lastName:$account:0" >> $file
    echo "添加账户$account成功!!!"
  else
    echo "已存在该用户!!!"
  fi
}
function menu4()
{
  read -p "请输入删除账户:" account
  # 检查该账户是否是6位
  if [ `expr length $account` != 6 ]
  then
    echo "输入账户必须为6位整数!!!"
    return
  fi
  # 检查该账户是否是整数
  expr $account + 0 > /dev/null
  if [ $? -ne 0 ]
  then
    echo "输入账户必须为6位整数11!!!"
    return
  fi
  # 检查是否有该账户
  result=`sed -n "/:$account:/p" $file`
  if [ "$result" = "" ]
  then
    echo "未查找到该账户!!!"
  else
    sed -i "/:$account:/d" $file
    echo "删除账户$account成功"
  fi
}
function menu5()
{
  read -p "请输入查询的账户:" account
  # 检查该账户是否是6位
  if [ `expr length $account` != 6 ]
  then
    echo "输入账户必须为6位整数!!!"
    return
  fi
  # 检查该账户是否是整数
  expr $account + 0 > /dev/null
  if [ $? -ne 0 ]
  then
    echo "输入账户必须为6位整数11!!!"
    return
  fi
  # 检查是否有该账户
  result=`sed -n "/:$account:/p" $file`
  if [ "$result" = "" ]
  then
    echo "未查找到该账户!!!"
  else
    echo "查询到账户信息"
    sed -n "/:$account:/p" $file > result 
    echo $result | awk -F ':' '{ print "First name: " $1 \
                                       "  Middle name: " $2 \ 
                                       "  Last name name: " $3 \
                                       "  账户: " $4 \
                                       "  余额: "$5 }'
  fi
}
function menu6()
{
  index=0
  # 循环读取每一行
  cat $file | while read line
  do
  # 第一行是字段标题,直接跳过
  if [ $index -eq 0 ]
  then
    index=`echo "$index+1" | bc`
    continue
  fi
  # 输出每一行
  echo $line | awk -F ':' '{ print "First name: " $1 \
                                 "  Middle name: " $2 \ 
                                 "  Last name name: " $3 \
                                 "  账户: " $4 \
                                 "  余额: " $5 }'
  done
}
while true
do
  showMenu;
  if [ "$number" = "" ]
  then
    echo "输入错误!!!"
  elif [ $number -eq 0 ]
  then
    exit
  elif [ $number -eq 1 ]
  then
    menu1;
  elif [ $number -eq 2 ]
  then
    menu2;
  elif [ $number -eq 3 ]
  then
    menu3;
  elif [ $number -eq 4 ]
  then
    menu4;
  elif [ $number -eq 5 ]
  then
    menu5;
  elif [ $number -eq 6 ]
  then
    menu6;
  else
    echo "输入错误!!!"
  fi
done
exit



相关文章
|
1月前
|
Shell Linux API
【Shell 命令集合 备份压缩 】Linux 解压缩文件 unzip命令 使用指南
【Shell 命令集合 备份压缩 】Linux 解压缩文件 unzip命令 使用指南
56 0
|
1月前
|
存储 监控 Linux
【Shell 命令集合 系统管理 】⭐⭐⭐Linux 查看当前正在运行的进程信息 ps命令 使用指南
【Shell 命令集合 系统管理 】⭐⭐⭐Linux 查看当前正在运行的进程信息 ps命令 使用指南
42 0
|
1月前
|
Shell Linux C语言
【Shell 命令集合 系统设置 】Linux 创建Kickstart文件mkkickstart命令 使用指南
【Shell 命令集合 系统设置 】Linux 创建Kickstart文件mkkickstart命令 使用指南
31 0
|
1月前
|
Shell Linux 编译器
【Shell 命令集合 备份压缩 】Linux 提取zip压缩文件的详细信息 zipinfo命令 使用指南
【Shell 命令集合 备份压缩 】Linux 提取zip压缩文件的详细信息 zipinfo命令 使用指南
34 0
|
1月前
|
存储 Shell Linux
【Shell 命令集合 备份压缩 】Linux 解码uuencode编码的文件 uudecode 命令 使用指南
【Shell 命令集合 备份压缩 】Linux 解码uuencode编码的文件 uudecode 命令 使用指南
31 0
|
1月前
|
存储 Unix Shell
【Shell 命令集合 系统管理 】⭐⭐Linux 显示系统的基本信息 uname命令 使用指南
【Shell 命令集合 系统管理 】⭐⭐Linux 显示系统的基本信息 uname命令 使用指南
34 1
|
1月前
|
监控 Shell Linux
【Shell 命令集合 系统管理 】Linux 自动轮转(log rotation)日志文件 logrotate命令 使用指南
【Shell 命令集合 系统管理 】Linux 自动轮转(log rotation)日志文件 logrotate命令 使用指南
51 0
|
1月前
|
存储 Linux Shell
【Shell 命令集合 系统设置 】Linux 获取指定模块的元信息 minfo命令 使用指南
【Shell 命令集合 系统设置 】Linux 获取指定模块的元信息 minfo命令 使用指南
30 0
|
3天前
|
Java 关系型数据库 MySQL
Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
【4月更文挑战第12天】Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
28 3
|
2天前
|
Shell Android开发
Android系统 adb shell push/pull 禁止特定文件
Android系统 adb shell push/pull 禁止特定文件
14 1