SHELL菜單

SHELL菜單
2011年09月01日
   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 [ $1 = "0" ]
  then
  expression="\$1 ~ /^.\$/"
  else
  expression="\$1 ~ /^$1-.\$/"
  fi
  awk -v BLANK="$blank" -v ETCFILE="$etcfile" -v MENUID="$1" \
  'BEGIN{\
  while ( getline tracelog > 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 [ $__i -eq 2 ]
  then
  title=$__p
  elif [ $__i -eq 3 ]
  then
  cmd=$__p
  elif [ $__i -eq 4 ]
  then
  redoflg=$__p
  elif [ $__i -eq 5 ]
  then
  dependflg=$__p
  fi
  done
  }
  submenu(){
  currlevel=`expr $currlevel + 1`
  currmenu="$current"
  }
  __upmenu(){
  currlevel=`expr $currlevel - 1`
  if [ "$currlevel" = "1" ]
  then
  currmenu="0"
  else
  __len=`expr $currlevel \* 2 - 3`
  currmenu=`printf "%."$__len"s" $currmenu`
  fi
  }
  __getcmd(){
  read choice
  if [ "X$choice" = "Xq" ]
  then
  if [ "X$currlevel" = "X1" ]
  then
  cmd="QUIT"
  else
  cmd="__upmenu"
  fi
  else
  if [ "X$currlevel" = "X1" ]
  then
  current=$choice
  else
  __len=`expr $currlevel \* 2 - 3`
  current=`printf "%."$__len"s" $current`"-$choice"
  fi
  __getfunc
  fi
  }
  __cmdcheck(){
  if [ "X$cmd" = "Xsubmenu" -o "X$cmd" = "X__upmenu" ]
  then
  return 1
  fi
  __statuslist=`awk -v depend=$dependflg '{if ($1 == depend)print $2}' $etcfile`
  __status=`echo $__statuslist | awk '{print $NF}'`
  if [ ! "X$dependflg" = "X" -a ! "X$__status" = "X0" ]
  then
  errmsg=`echo "該步依賴於步驟[$dependflg],但此步未執行或執行失敗"`
  return 0
  fi
  __statuslist=`awk -v current=$current '{if ($1 == current)print $2}' $etcfile`
  __status=`echo $__statuslist | awk '{print $NF}'`
  if [ "X$redoflg" = "X0" -a "X$__status" = "X0" ]
  then
  errmsg=`echo "該步不能重複執行"`
  return 0
  fi
  return 1
  }
  __exec(){
  if [ ! "X$cmd" = "X" ]
  then
  __cmdcheck
  if [ $? -eq 1 ]
  then
  $cmd
  status=$?
  if [ ! "X$cmd" = "Xsubmenu" -a ! "X$cmd" = "X__upmenu" ]
  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 [ ! -f $etcfile ]
  then
  > $etcfile
  fi
  while true
  do
  __showmenu $currmenu
  __getcmd
  if [ "X$cmd" = "XQUIT" ]
  then
  printf "確定要退出嗎(y/n)?"
  read __ok
  if [ "X$__ok" = "Xy" ]
  then
  break
  else
  continue
  fi
  elif [ "X$cmd" = "XE" ]
  then
  continue
  else
  __exec
  fi
  done
  rm -f $etcfile
  ------------------------end----------------------- -----------
  
  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章