linux shell 编写菜单脚本事例

简介:
menu2文件代码:
---
#!/bin/sh
#menu2
#Main menu script
#ignore ctrl-c and QUIT interrupts
trap "" 2 3 15
MYDATE=`date +%d/%m/%Y`
THIS_HOST=`hostname -s`
USER=`whoami`
#user level file
USER_LEVELS=priv.user
#hold file
HOLD1=hold1.$$
#colour function
function colour ()
{
  case $1 in
    black_green)
       echo '[40;32m'
       ;;
    black_yellow)
       echo '[40;33m'
       ;;
    black_white)
       echo '[40;37'
       ;;
    black_cyan)
       echo '[40;36m'
       ;;
    red_yellow)
       echo '[41;33m'
       ;;
   esac
}
#just read a single key please
function get_char()
{
  #get_char
  #save current stty setting 
  SAVEDSTTY=`stty -g`
  stty cbreak
  dd if=/dev/tty bs=1 count=1 2>/dev/null
  stty -cbreak 
  #restore stty
  stty $SAVEDSTTY
}
#turn the cursor on or off
function cursor ()
{
  #turn cursor on/off
  _OPT=$1
  case $_OPT in
   on ) echo '[?25h'
        ;;
   off) echo '[?25l'
        ;;
   *) return 1 
        ;;
   esac
}
#check what privilege level the user has
function restrict ()
{
   colour red_yellow
   echo -e -n "\n\n\007 Sorry your are not authorised to use this function"
   colour black_green
}
function user_level ()
{
  #read in the priv.user file
  while read LINE
  do 
    case $LINE in
      #ignore comments
    \#*) ;;
      *) echo $LINE>>$HOLD1
         ;;
     esac
  done < $USER_LEVELS
  FOUND=false
  while read MENU_USER PRIV
  do 
    if [ "$MENU_USER" = "$USER"  ] ; then
      FOUND=true
      case $PRIV in
        yes|YES)
           return 0
           ;;
        no|NO)
           return 1
           ;;
      esac
    else
    #no match found read next record 
     continue
     fi
   done <$HOLD1
   if [ "$FOUND" = "false" ] ; then
      echo "Sorry $USER  you have not been authorsied to use this menu" 
      exit 1
    fi    
}
#called when user selects quit
function my_exit ()
{
  colour black_white
  cursor on
  rm *.$$
  exit 0
}
tput init 
#display their user levels on the screen
if user_level ; then
ACCESS="Access Mode is High"
else
ACCESS="Access Mode is Normal"
fi
tput init 
while :
do
tput clear 
colour black_green
cat <<MAYDAY
$ACCESS
---------------------------------------------------------------------
User:$USER                 Host:$THIS_HOST            Date:$MYDATE
---------------------------------------------------------------------
                   1:ADD A RECORD
                   2:VIEW A RECORD
                   3:PAGE ALL RECORD
                   4:CHANGE A RECORD
                   5:DELETE A RECORD
                   P:PRINT ALL RECORDS
                   H:Help screen
                   Q:Exit Menu
---------------------------------------------------------------------
MAYDAY
colour black_cyan
echo -e -n "\tYour Choice [1,2,3,4,5,P,H,Q] >"
CHOICE=`get_char`
 case $CHOICE in
  1) ls
     ;;
  2) vi
     ;;
  3) who 
     ;;
  4) if user_level; then
       ls -l | wc
     else
       restrict 
      fi
      ;; 
  5) if user_level;then
       sort  /etc/passwd
     else
       restrict
     fi
       ;;
   P|p) echo -e "\n\nPrinting recirds ......"
       ;;
   H|h)
   tput clear 
   cat <<MAYDAY
This is the help screen ,nothing here yet to help you!
MAYDAY
;;
    Q|q) my_exit
       ;;
    *) echo -e "\t\007unknown user respense"
       ;;
     esac
echo -e -n "\tHit the return key to continue"
read DUMMY
done
 
---
priv.user 文件代码:
#root quanxian wei  YES, daia quanxianwei no
root   yes
daia   no


本文转自守住每一天51CTO博客,原文链接:http://blog.51cto.com/liuyu/258791,如需转载请自行联系原作者
相关文章
|
10月前
|
算法 Linux Shell
Linux实用技能:打包压缩、热键、Shell与权限管理
本文详解Linux打包压缩技巧、常用命令与原理,涵盖.zip与.tgz格式操作、跨系统传文件方法、Shell运行机制及权限管理,助你高效使用Linux系统。
Linux实用技能:打包压缩、热键、Shell与权限管理
|
10月前
|
存储 安全 Unix
七、Linux Shell 与脚本基础
别再一遍遍地敲重复的命令了,把它们写进Shell脚本,就能一键搞定。脚本本质上就是个存着一堆命令的文本文件,但要让它“活”起来,有几个关键点:文件开头最好用#!/usr/bin/env bash来指定解释器,并用chmod +x给它执行权限。执行时也有讲究:./script.sh是在一个新“房间”(子Shell)里跑,不影响你;而source script.sh是在当前“房间”里跑,适合用来加载环境变量和配置文件。
872 9
|
10月前
|
存储 Shell Linux
八、Linux Shell 脚本:变量与字符串
Shell脚本里的变量就像一个个贴着标签的“箱子”。装东西(赋值)时,=两边千万不能有空格。用单引号''装进去的东西会原封不动,用双引号""则会让里面的$变量先“变身”再装箱。默认箱子只能在当前“房间”(Shell进程)用,想让隔壁房间(子进程)也能看到,就得给箱子盖个export的“出口”戳。此外,Shell还自带了$?(上条命令的成绩单)和$1(别人递进来的第一个包裹)等许多特殊箱子,非常有用。
940 2
|
12月前
|
Web App开发 缓存 安全
Linux一键清理系统垃圾:释放30GB空间的Shell脚本实战​
这篇博客介绍了一个实用的Linux系统盘清理脚本,主要功能包括: 安全权限检查和旧内核清理,保留当前使用内核 7天以上日志文件清理和系统日志压缩 浏览器缓存(Chrome/Firefox)、APT缓存、临时文件清理 智能清理Snap旧版本和Docker无用数据 提供磁盘空间使用前后对比和大文件查找功能 脚本采用交互式设计确保安全性,适合定期维护开发环境、服务器和个人电脑。文章详细解析了脚本的关键功能代码,并给出了使用建议。完整脚本已开源,用户可根据需求自定义调整清理策略。
1271 1
|
Linux Shell
在Linux、CentOS7中设置shell脚本开机自启动服务
以上就是在CentOS 7中设置shell脚本开机自启动服务的全部步骤。希望这个指南能帮助你更好地管理你的Linux系统。
2287 25
|
Linux Shell
shell_42:Linux参数移动
总的来说,参数移动是Linux shell脚本中的一个重要概念,掌握它可以帮助我们更好地处理和管理脚本中的参数。希望这个解释能帮助你理解和使用参数移动。
351 18
|
Linux Shell
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
416 4
|
Linux Shell 数据安全/隐私保护
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
679 3
|
Shell Linux
【linux】Shell脚本中basename和dirname的详细用法教程
本文详细介绍了Linux Shell脚本中 `basename`和 `dirname`命令的用法,包括去除路径信息、去除后缀、批量处理文件名和路径等。同时,通过文件备份和日志文件分离的实践应用,展示了这两个命令在实际脚本中的应用场景。希望本文能帮助您更好地理解和应用 `basename`和 `dirname`命令,提高Shell脚本编写的效率和灵活性。
1529 32
|
存储 Shell Linux
Linux 如何更改默认 Shell
Linux 如何更改默认 Shell
1461 0
Linux 如何更改默认 Shell