shell作业项目留存代码

简介: shell作业项目留存代码

完整代码

初始化函数【init.sh】

function initHotel(){
  # 判断文件夹是否存在
  file=hotel
  if [ ! -d "$file" ]
  then
    mkdir hotel
    echo "初始化文件夹成功"
  fi
  file_f="hotel/f1n1.txt"
  # 清空变量
  unset isf
  if [ -f "$file_f" ]
  then
    read -p "是否覆盖初始化?(yes/no)" isf
    if [ $isf = "no" ]
    then
      echo "取消初始化,保留原数据."
      sleep 1
      # 取消初始化
      return 0
    else
      # 清空文件夹
      rm -rf hotel/
      # 重新创建文件夹以及对应房间
      mkdir hotel
      # 创建房间
      createFile
    fi
  else
    # 创建房间
    createFile
  fi
  # 返回1代表完成
  return 1
}
function createFile(){
  for((i=1;i<=5;i++))
  do
    for((j=1;j<10;j++))
    do
      touch "hotel/f"$i"n"$j".txt"
    done
  done
}
initHotel

查询房间【finds.sh】

function finds(){
  # 累积是否换行次数
  count=0
  for((i=1;i<=5;i++))
  do
    for((j=1;j<10;j++))
    do
      ((count++))
      file="f"$i"n"$j".txt"
      # 空
      if [ ! -s "hotel/$file" ] && [ $1 == 0 ]
      then
        echo -n -e $file"\t"
        if ((count%5==0))
        then
          echo
        fi
        continue
      fi
      # 非空
      if [ -s "hotel/$file" ] && [ $1 == 1 ]
      then
        echo -n -e $file"\t"
        if ((count%5==0))
        then 
          echo
        fi
      fi
    done
  done
  echo
}
finds $1

入住房间【inInfo.sh】

read -p "请输入入住房号(例:f1n1):" fno
read -p "请输入入住人姓名:" name
read -p "请输入入住人手机号:" phone
read -p "请输入入住人身份证号码:" idcard
echo "$name - $phone -$idcard">"hotel/$fno".txt
echo "$name 入住完毕 入住房间$fno"
echo "$name - $phone -$idcard 入住 $fno">>hotel.log
taike@taikeEDU:~/桌面/fun$ vi admin.sh

退出房间【outInfo.sh】

read -p "请输入退房房号(例:f2n7):" nums
info=$(cat hotel/$nums.txt)
echo "$info 退房 $nums">>hotel.log
echo "正在检查房间..."
sleep 1
echo "退房完毕。"
>"hotel/$nums".txt

运行主程序【main.sh】

clear
echo "--------------------------"
echo "-----欢迎来到豪华酒店-----"
echo "--------------------------"
# 初始化房间
source init.sh
while :
do
  echo "-----1、查看未住房间------"
  echo "-----2、查看入住房间------"
  echo "-----3、房间入住操作------"
  echo "-----4、房间退房操作------"
  echo "-----5、退出操作系统------"
  echo "--------------------------"
  read -p "请输入操作选项:" nums
  case $nums in
    1)
      source finds.sh 0
      ;;
    2)
      source finds.sh 1
      ;;
    3)  
      source inInfo.sh
      ;;
    4)  
      source outInfo.sh
      ;;
    5)echo "欢迎下次继续使用!"
      exit 
      ;;
  esac
done

留存日志【log.log】

功能演示

功能1

功能3房间入住操作

功能4房间退房操作

功能2查看入住房间

功能5退出操作系统

日志查看【hotel.log】

相关文章
|
10月前
|
存储 Shell Linux
C语言模拟实现Liunx操作系统与用户之间的桥梁shell(代码详解)
C语言模拟实现Liunx操作系统与用户之间的桥梁shell(代码详解)
112 1
C语言模拟实现Liunx操作系统与用户之间的桥梁shell(代码详解)
|
2月前
|
Shell 开发工具 git
使用 Shell 代码简化 Git 步骤
【8月更文挑战第23天】本文介绍通过Shell脚本简化Git操作的方法:1) 使用`gitc &quot;提交信息&quot;`可一键完成代码提交与推送至远程仓库;2) 执行`gitpull`即可从远程仓库拉取最新代码并合并到当前分支;3) 输入`gitnewbranch 分支名称`快速创建并切换到新分支。将这些自定义函数加入`.bashrc`或`.zshrc`等配置文件后,即可随时调用简化版Git命令。
|
3月前
|
Shell 应用服务中间件 Linux
shell 实现项目的启动与停止
shell 实现项目的启动与停止
21 0
|
5月前
|
Shell Linux
【Linux】进程实践项目(更新中) — 自主shell编写
前几篇文章,我们学习进程的相关知识:进程概念,进程替换,进程控制。熟悉了进程到底是个什么事情,接下来我们来做一个实践,来运用我们所学的相关知识。这个项目就是手搓一个shell模块,模拟实现Xshell中的命令行输入。
48 1
|
5月前
|
Java Shell API
通用Shell脚本执行Spring Boot项目Jar包
通用Shell脚本执行Spring Boot项目Jar包
|
Shell 开发工具 git
教你写一个快速提交git代码的shell脚本(二)
教你写一个快速提交git代码的shell脚本(二)
|
5月前
|
Java Unix Shell
springboot项目重启的shell命令
springboot项目重启的shell命令
|
Shell
shell作业(一)
shell作业(一)
|
5月前
|
Unix Shell Linux
shell脚本转换为c代码的工具
将shell脚本转换成代码,并编译成二进制可执行文件,在linux系统下具有防调试跟踪机制
教你写一个快速提交git代码的shell脚本(一)
教你写一个快速提交git代码的shell脚本(一)
教你写一个快速提交git代码的shell脚本(一)