练习一

  1、写一个脚本

1)显示一个菜单给用户

d|D) show disk usages.

m|M) show memory usages.

s|S) show swap usages.

*) quit.

2) 当用户给定选项后显示相应的内容:

扩展:

     当用户选择完成,显示相应信息后,不退出;而让用户再一次选择,再次

显示相应的内容;除了用户使用quit.

#!/bin/bash

#program:

#练习磁盘管理相关脚本编写

#history  donggen  2016-11-03-10:19

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bash

export PATH

cat << EOF

d|D) show disk usages.

m|M) show memory usages.

s|S) show swap usages.

*) quit.

EOF

 read -p "Your choice:" CHOICE

   while [ $CHOICE != "quit" ]; do

      case $CHOICE in 

          d|D)

             df -Ph  ;;

          m|M)

             free -m  ;;

          s|S) 

             free -m | grep 'Swap'  ;;

          *)

             echo "Unknown.."  ;;

       esac

    read -p "Again,Your choice:" CHOICE

  done

[root@xuelinux test]# ./showsystem.sh 

d|D) show disk usages.

m|M) show memory usages.

s|S) show swap usages.

*) quit.

Your choice:d

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda2       4.9G  1.5G  3.1G  33% /

tmpfs           947M     0  947M   0% /dev/shm

/dev/sda1       2.0G   61M  1.8G   4% /boot

/dev/sda3       3.9G   73M  3.6G   2% /home

/dev/sda7       2.0G   35M  1.8G   2% /tmp

/dev/sda5       2.9G  1.7G  1.2G  59% /usr

Again,Your choice:D

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda2       4.9G  1.5G  3.1G  33% /

tmpfs           947M     0  947M   0% /dev/shm

/dev/sda1       2.0G   61M  1.8G   4% /boot

/dev/sda3       3.9G   73M  3.6G   2% /home

/dev/sda7       2.0G   35M  1.8G   2% /tmp

/dev/sda5       2.9G  1.7G  1.2G  59% /usr

Again,Your choice:S

Swap:         1999          0       1999

Again,Your choice:N

Unknown..

Again,Your choice:M

             total       used       free     shared    buffers     cached

Mem:          1893        181       1711          0         42         62

-/+ buffers/cache:         76       1816

Swap:         1999          0       1999

Again,Your choice:quit     

[root@xuelinux test]#