Linux&shell之高级Shell脚本编程-创建菜单

写在前面:案例、常用、归类、解释说明。(By Jim)

创建菜单
#!/bin/bash
# testing the script
clear
echo
echo -e "\t\t\tSys Admin Menu\n"
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"
echo -en "\t\tEnter option:"
(这段代码很有意思,会显示目录的效果)

创建菜单函数
function diskspace {
  clear
  df -k
}

function whoseon {
  clear
  who
}

function memusage {
  clear
  cat /proc/meminfo
}
添加菜单逻辑
case $option in
0)
  break ;;
1)
  diskspace ;;
2)
  whoseon ;;
3)
  menusage ;;
*)
  clear
  echo "Sorry,wrong selection" ;;
esac

完整的菜单如下:

#!/bin/bash
# testing the script
function diskspace {
  clear
  df -k
}

function whoseon {
  clear
  who
}

function memusage {
  clear
  cat /proc/meminfo
}

function menu {
  clear
  echo
  echo -e "\t\t\tSys Admin Menu\n"
  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"
  echo -en "\t\tEnter option:"
  read -n 1 option
}
while [ 1 ]
do
menu
case $option in
0)
  break ;;
1)
  diskspace ;;
2)
  whoseon ;;
3)
  menusage ;;
*)
  clear
  echo "Sorry,wrong selection" ;;
esac
echo -en "\n\n\t\tHit any key to continue"
read -n 1 line
done
clear

你可能感兴趣的:(shell脚本)