shell指令碼菜單的建立

來源:互聯網
上載者:User
 

建立文本菜單建立菜單布局

root@wl-MS-7673:/home/wl/案頭/shell# cat -n test1.sh      1    #!/bin/bash       2    echo -e "\t\tMenu"       3    echo -e "1.\tDisplay disk space"       4    echo -e "2.\tDisplay logged on user"       5    echo -e "3.\tDisplay memory usage"       6    echo -e "0.\tExit menu\n\n"       7    echo -en "\t\tEnter option:" #-n表示不列印斷行符號       8    read -n1 option #讀取一個字元       9    echo ""  root@wl-MS-7673:/home/wl/案頭/shell# ./test1.sh Menu1.Display disk space2.Display logged on user3.Display memory usage0.Exit menuEnter option:1root@wl-MS-7673:/home/wl/案頭/shell# 



建立菜單函數

只需把上面的代碼用函數包起來即可

添加菜單邏輯添加對應的case即可


root@wl-MS-7673:/home/wl/案頭/shell# cat -n test2.sh      1    #!/bin/bash       2    option=""       3    create_menu(){                4        echo -e "\t\tMenu"       5        echo -e "1.\tDisplay disk space"       6        echo -e "2.\tDisplay logged on user"       7        echo -e "3.\tDisplay memory usage"       8        echo -e "0.\tExit menu\n\n"       9        echo -en "\t\tEnter option:"      10        read -n1 option      11        echo ""      12    }      13          14    create_menu      15          16    case $option in      17    0)      18    echo "Display disk space";;      19    1)      20    echo "Display logged on user";;      21    2)      22    echo "Display memory usage";;      23    3)      24    echo "Exit menu";;      25    *)      26    echo "error";;      27    esac      28root@wl-MS-7673:/home/wl/案頭/shell# ./test2.sh Menu1.Display disk space2.Display logged on user3.Display memory usage0.Exit menuEnter option:3Exit menuroot@wl-MS-7673:/home/wl/案頭/shell# apt-get install sed




root@wl-MS-7673:/home/wl/案頭/shell# cat test3.sh #/bin/bash   ption=""  create_menu(){       echo -e "\t\tMenu"      echo -e "1.\tDisplay disk space"      echo -e "2.\tDisplay logged on user"      echo -e "3.\tDisplay memory usage"      echo -e "0.\tExit menu\n\n"      echo -en "\t\tEnter option:"      read -n1 option      echo ""  }    create_menu   disk_space(){            df -k      }            whoseon(){              who      }            menu_usage(){              cat /proc/meminfo      }     deal_menu(){          case $option in          1)          disk_space;;          2)          whoseon;;          3)          menu_usage;;          0)          echo "Exit menu";;          *) #輸入錯誤,重新輸入          echo -e "\nSorry, wrong selection."          echo -en "\n\n\t\tHit any key to continue."          read -n1 option #重新讀取菜單選項          deal_menu;;          esac      }            deal_menu   root@wl-MS-7673:/home/wl/案頭/shell# 
運行結果如圖:

root@wl-MS-7673:/home/wl/案頭/shell# ./test3.sh Menu1.Display disk space2.Display logged on user3.Display memory usage0.Exit menuEnter option:



root@wl-MS-7673:/home/wl/案頭/shell# ./test3.sh Menu1.Display disk space2.Display logged on user3.Display memory usage0.Exit menuEnter option:1檔案系統          1K-塊    已用     可用 已用% 掛載點/dev/sda11     68151872 7654680 57035204   12% /udev            2039652       4  2039648    1% /devtmpfs            818784     908   817876    1% /runnone               5120       0     5120    0% /run/locknone            2046956     264  2046692    1% /run/shmroot@wl-MS-7673:/home/wl/案頭/shell# 

 
 

使用select命令


root@wl-MS-7673:/home/wl/案頭/shell# cat -n test4.sh     1#!/bin/bash      2     3     4disk_space(){       5       6        df -k       7    }       8           9    whoseon(){      10        11        who      12    }      13          14    menu_usage(){      15        16        cat /proc/meminfo      17    }     18    19    20    21    PS3="Enter option:"      22    select option in "Display disk space" "Display logged on user" "Display memory usage" "Exit menu"      23    do      24        case $option in      25        "Display disk space")      26        disk_space;;      27        "Display logged on user")      28        whoseon;;      29        "Display memory usage")      30        menu_usage;;      31        "Exit menu")      32        echo "Exit menu"      33        break;;      34        *)      35        echo -e "\nSorry, wrong selection."      36        echo -en "\n\n\t\tHit any key to continue."      37        read -n1 option      38        deal_menu;;      39        esac      40    done  root@wl-MS-7673:/home/wl/案頭/shell# 


 

運行結果如圖所示:

root@wl-MS-7673:/home/wl/案頭/shell# ./test4.sh 1) Display disk space   3) Display memory usage2) Display logged on user  4) Exit menuEnter option:1檔案系統          1K-塊    已用     可用 已用% 掛載點/dev/sda11     68151872 7654632 57035252   12% /udev            2039652       4  2039648    1% /devtmpfs            818784     908   817876    1% /runnone               5120       0     5120    0% /run/locknone            2046956     264  2046692    1% /run/shmEnter option:2wl       pts/0        2013-11-25 15:44 (:0.0)wl       pts/1        2013-11-25 17:31 (:0.0)Enter option:3MemTotal:        4093912 kBMemFree:         2341120 kBBuffers:          187220 kBCached:           702080 kBSwapCached:            0 kBActive:           917904 kBInactive:         521664 kBActive(anon):     551016 kBInactive(anon):     2088 kBActive(file):     366888 kBInactive(file):   519576 kBUnevictable:          16 kBMlocked:              16 kBHighTotal:       3253080 kBHighFree:        1780332 kBLowTotal:         840832 kBLowFree:          560788 kBSwapTotal:       4165628 kBSwapFree:        4165628 kBDirty:                92 kBWriteback:             0 kBAnonPages:        550372 kBMapped:           218440 kBShmem:              2840 kBSlab:              66112 kBSReclaimable:      47724 kBSUnreclaim:        18388 kBKernelStack:        3488 kBPageTables:         8988 kBNFS_Unstable:          0 kBBounce:                0 kBWritebackTmp:          0 kBCommitLimit:     6212584 kBCommitted_AS:    2815796 kBVmallocTotal:     122880 kBVmallocUsed:       30336 kBVmallocChunk:      92168 kBHardwareCorrupted:     0 kBAnonHugePages:         0 kBHugePages_Total:       0HugePages_Free:        0HugePages_Rsvd:        0HugePages_Surp:        0Hugepagesize:       2048 kBDirectMap4k:        8184 kBDirectMap2M:      905216 kBEnter option:4Exit menuroot@wl-MS-7673:/home/wl/案頭/shell# 


在指令碼中使用dialog命令




root@wl-MS-7673:/home/wl/案頭/shell# cat -n test5.sh      1    #!/bin/bash       2    temp=`mktemp -t temp.XXXXXX`       3    temp2=`mktemp -t temp2.XXXXXX`       4    disk_space(){       5        df -k > $temp       6        dialog --textbox $temp 20 60       7    }       8           9    whoseon(){      10        who > $temp      11        dialog --textbox $temp 20 50      12    }      13          14    menu_usage(){      15        cat /proc/meminfo > $temp      16        dialog --textbox $temp 20 50      17    }      18          19    dialog --menu "menu" 20 30 10 1 "Display disk space" 2 "Display logged on user" 3 "Display memory usage" 0 "Exit menu" 2>$temp2      20    if [ $? -ne 1 ]      21    then      22        selection=`cat $temp2`      23        case $selection in      24            1)      25                disk_space;;      26            2)      27                whoseon;;      28            3)      29                menu_usage;;      30            0)    ;;      31            *)      32            dialog --msgbox "Sorry, invalid selection" 10 30      33        esac      34    fi      35          36    rm -f $temp $temp2 2>/dev/null  root@wl-MS-7673:/home/wl/案頭/shell# 

運行結果與上述類似。



zenity --calendar





root@wl-MS-7673:/home/wl/案頭/shell# cat -n test6.sh     1#!/bin/bash       2temp=`mktemp -t temp.XXXXXX`       3temp2=`mktemp -t temp2.XXXXXX`       4disk_space(){       5    df -k > $temp       6    zenity --text-info --title "Disk space" --filename=$temp --width 750 --height 300       7}       8       9whoseon(){      10    who > $temp      11    zenity --text-info --title "Logged on user" --filename=$temp --width 500 --height 200      12}      13      14menu_usage(){      15    cat /proc/meminfo > $temp      16    zenity --text-info --title "Memory usage" --filename=$temp --width 300 --height 500      17}      18      19zenity --list --radiolist --title "Menu" --column "Select" \      20--column "Menu Item" FALSE "Display disk space" FALSE "Display logged on user" FALSE "Display memory usage" FALSE "Exit" > $temp2      21if [ $? -ne 1 ]      22then      23    selection=`cat $temp2`      24    case $selection in      25        "Display disk space")      26            disk_space;;      27        "Display logged on user")      28            whoseon;;      29        "Display memory usage")      30            menu_usage;;      31        "Exit")    ;;      32        *)      33        zenity --info "Sorry. invalid selection."      34    esac      35fi      36      37rm -f $temp $temp2 2>/dev/null root@wl-MS-7673:/home/wl/案頭/shell# 




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.