美文网首页
shell编程-交互 脚本菜单

shell编程-交互 脚本菜单

作者: 拾忆泡沫 | 来源:发表于2017-11-18 14:32 被阅读0次

shell编程交互 脚本菜单

shell脚本的交互最常用的方式是使用菜单,通常是echo打印菜单出来。

[root@db ~]# cat echomen.sh 
#!/bin/bash
#simple script menu
function diskapace {
clear
df -k
}

function whoseon {
clear
who
}

function menusage {
clear
cat /proc/meninfo
}

function menu {
clear 
echo
echo -e "\t\t\t test menu"
echo -e "\t1. Display disk space"
echo -e "\t2. Display logged on users" 
echo -e "\t3. Display memory usage" 
echo -e "\t0. Exit menu\n\n"
#-en 选项会去掉末尾的换行符,这让菜单看起来更专业一些
echo -en "\t\tEnter option:" 
#read 命令读取用户输入
read -n 1 option
}

while [ 1 ]
do 
    menu
    case $option in
    0)
        break ;;
    1)
        diskapace  ;;
    2)
        whoseon ;;
    3)
        menusage ;;
    *)
        clear
        echo "sorry,wrong selection" ;;
    esac
    echo -en "\n\n\t\thit any to contunue"
    read -n 1 line
done
clear
运行:

             test menu
    1. Display disk space
    2. Display logged on users
    3. Display memory usage
    0. Exit menu

        Enter option:

上面的脚本基本上是用echo打印的时间都花在写菜单,为此bash shell提供了一个命令select。
select命令只需要一条命令就可以创建菜单,并获取用户输入,命令格式

select variable in list
do
    commands
done

list是由空格组成的文本列表
上面的脚本可以修改为以下这种形式:

#!/bin/bash
#simple script menu
function diskapace {
clear
df -k
}

function whoseon {
clear
who
}

function menusage {
clear
cat /proc/meninfo
}

select option in "Display disk space" "Display logged on users" "Display memory usage" "Exit menu"
do 
    case $option in
    "Exit menu")
        break ;;
    "Display disk space")
        diskapace  ;;
    "Display logged on users")
        whoseon ;;
    "Display memory usage")
        menusage ;;
    *)
        clear
        echo "sorry,wrong selection" ;;
    esac
done
clear
运行:
# ./selectmeun.sh 
1) Display disk space       3) Display memory usage
2) Display logged on users  4) Exit menu
#? 

可以把菜单选项赋值到变量中,动态的生成菜单项。
例如根据/root目录下的文件修改时间,动态的最新修改的8个文件名菜单项

#!/bin/bash
#test select option men 
menu=`ls -t /root/menu | cat | sed -n "1,8p"`

select option in ${menu} "Exit menu"
do
        echo ${option}
done
#ls /root/menu
test1  test2  test3  test4  test5  test6  test7  test8  test9

( -t :用文件和目录的更改时间排序)
#ls -t /root/menu
test9  test8  test7  test6  test4  test5  test3  test2  test1

#./dynamicmenu.sh
[root@db ~]# ./men.sh 
1) test9      3) test7      5) test4      7) test3  9) Exit menu
2) test8      4) test6      6) test5      8) test2
#? 

如果删除一个文件或者增加一个文件,再次执行时菜单项就会变化
删除文件test3 、test5并且创建test10文件后再次执行脚本菜单变化了
1) test10     3) test8      5) test6      7) test2  9) Exit menu
2) test9      4) test7      6) test4      8) test1
#? 

注意:在使用select命令中,存储的变量值是菜单中的文本字符串而不是菜单选项中相关联的数字。

相关文章

  • shell编程-交互 脚本菜单

    shell编程交互 脚本菜单 shell脚本的交互最常用的方式是使用菜单,通常是echo打印菜单出来。 上面的脚本...

  • 8-JShell工具

        Shell是脚本程序的含义,在很多的编程语言里面为了方便使用者进行代码的开发,都会有Shell交互编程环境...

  • 78.shell编程

    shell编程 Shell脚本,是一种为shell编写的脚本程序。 Shell编程跟JavaScript、Pyth...

  • 【shell笔记>脚本】使用shell创建文本菜单和窗口部

    内容: 创建文本菜单创建文本窗口部件 创建文本菜单 创建交互式shell脚本最常用的方法是使用菜单,它提供了各种选...

  • shell编程

    Shell脚本,是一种为shell编写的脚本程序。 Shell编程跟JavaScript、Python编程一样,只...

  • shell脚本基础

    编写脚本 编程基础 shell脚本 创建shell脚本 变量 运算 测试 配置用户的环境

  • Unix/Linux shell入门

    一. 介绍 shell脚本,顾名思义就是跟执行shell命令、shell交互的脚本。由于历史原因,shell语法比...

  • Linux中expect工具完成远程交互通信(1)

    今天第一次在shell脚本中接触到expect工具,开启了脚本交互式的编程。在以前的脚本中,关于怎么远程执行指...

  • 编写Shell脚本---接受、判断用户参数

    [TOC] 编写Shell脚本 Shell脚本命令的工作方式分为两种:交互式和批处理交互式(Interactive...

  • mac终端下运行shell脚本

    From: 在mac下编写shell脚本并执行 一些资料 Shell教程-for 菜鸟教程 Shell脚本编程30...

网友评论

      本文标题:shell编程-交互 脚本菜单

      本文链接:https://www.haomeiwen.com/subject/jsuevxtx.html