---
title: "SHELL菜单"
author: "microjava"
date: "2009-12-15T10:45:56Z"
tags: []
categories: ["行业应用"]
slug: "shell"
draft: false
lastmod: "2009-12-15T10:45:56Z"
id: 548686
url: "https://microjava.iteye.com/blog/548686"
---

SHELL菜单的编写从此变得如此简单
完善的流程控制
简洁精美的界面

将下面的代码分别存为3个文件： msh.etc, msh.fun, msh.sh
执行msh.sh，看一下效果吧
只要修改msh.etc和msh.fun文件，就可以生成自己的菜单了，不用再写程序了

-------------------msh.etc-----------------------------
MAINMENU        超级SHELL主控菜单
1        子菜单        submenu
1-1        功能1        fun101        0
1-2        当前日期        fun102        0        1-1
2        功能函数2        fun2        1        1-1
-------------------end----------------------------------

-------------------msh.fun------------------------------
fun101(){
        echo "团结就是力量!!"
}
fun102(){
        date
}
fun2(){
        echo "神仙？妖怪?"
}
-------------------end--------------------------------------

-------------------msh.sh----------------------------------
#!/bin/sh

srcfile="./msh.etc"
funfile="./msh.fun"
etcfile="./.msh"
currlevel="1"
current=
currmenu="0"
choice=
title=
cmd=
redoflg=
dependflg=
status=
errmsg=
blank="                          "

. $funfile

__showmenu(){
        clear
        awk -v BLANK="$blank" '$1 ~ /^MAINMENU$/{printf "\n\n\n\n%s%s\n",BLANK,$2}' $srcfile
        printf "                    ----------------------------------\n"

        if 
        then
                expression="\$1 ~ /^.\$/"
        else
                expression="\$1 ~ /^$1-.\$/"
        fi

        awk -v BLANK="$blank" -v ETCFILE="$etcfile" -v MENUID="$1" \
        'BEGIN{\
                while ( getline tracelog < ETCFILE == 1 ){\
                        split(tracelog,menuid);\
                        idstatus]=menuid;\
                }\
        }\
        { \
                if ('"$expression"'){ \
                        if ($3!="submenu"){\
                                sign="*";\
                        }\
                        else{\
                                sign="";\
                        }\
                        if (idstatus=="0"){\
                                status="  √";\
                        }\
                        else if (idstatus==""){\
                                status="";\
                        }\
                        else{\
                                status="  @@";\
                        }\
                        print BLANK $1" "$2 sign status; \
                        lastid=$1;\
                } \
        }\
        END{\
                if ( idstatus=="" && idstatus=="0" ){\
                        print MENUID "        0" >> ETCFILE;
                }\
        }' $srcfile

        printf "${blank}q 退出\n"
        printf "请输入选择:"
}

__getfunc(){
        __i=0
        title=
        cmd=
        redoflg=
        dependflg=

        for __p in `awk '{ if ($1 ~ /^'"$current"'$/)print $0 }' $srcfile`
        do
                __i=`expr $__i + 1`
                if 
                then
                        title=$__p
                elif 
                then
                        cmd=$__p
                elif 
                then
                        redoflg=$__p
                elif 
                then
                        dependflg=$__p
                fi
        done
}

submenu(){
        currlevel=`expr $currlevel + 1`
        currmenu="$current"
}

__upmenu(){

        currlevel=`expr $currlevel - 1`

        if 
        then
                currmenu="0"
        else
                __len=`expr $currlevel \* 2 - 3`
                currmenu=`printf "%."$__len"s" $currmenu`
        fi
}

__getcmd(){

        read choice

        if 
        then
                if 
                then
                        cmd="QUIT"
                else
                        cmd="__upmenu"
                fi
        else
                if 
                then
                        current=$choice
                else
                        __len=`expr $currlevel \* 2 - 3`
                        current=`printf "%."$__len"s" $current`"-$choice"
                fi
                __getfunc
        fi
}

__cmdcheck(){

        if 
        then
                return 1
        fi

        __statuslist=`awk -v depend=$dependflg '{if ($1 == depend)print $2}' $etcfile`
        __status=`echo $__statuslist | awk '{print $NF}'`

        if 
        then
                errmsg=`echo "该步依赖于步骤,但此步未执行或执行失败"`
                return 0
        fi

        __statuslist=`awk -v current=$current '{if ($1 == current)print $2}' $etcfile`
        __status=`echo $__statuslist | awk '{print $NF}'`

        if 
        then
                errmsg=`echo "该步不能重复执行"`
                return 0
        fi

        return 1
}

__exec(){
        if 
        then
                __cmdcheck
                if 
                then
                        $cmd
                        status=$?

                        if 
                        then
                                printf "%s        %s        %s\n" $current        $status        `date +%Y/%m/%d-%H:%M:%S` >> $etcfile
                                echo "执行结束,按回车键返回"
                                read __a
                        fi
                else
                        echo "$errmsg"
                        read __a
                fi
        fi
}

if 
then
        > $etcfile
fi

while true
do

        __showmenu $currmenu

        __getcmd

        if 
        then
                printf "确定要退出吗(y/n)?"
                read __ok
                if 
                then
                        break
                else
                        continue
                fi
        elif 
        then
                continue
        else
                __exec
        fi

done

rm -f $etcfile
------------------------end----------------------------------