Creation of the Shell script menu

Source: Internet
Author: User
Tags memory usage

Create a text menu create a menu layout

root@wl-ms-7673:/home/wl/Desktop/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 USAG E "  
     6	    echo-e" 0.\texit menu\n\n "  
     7	    echo-en" \t\tenter option: "#-n indicates no print return  
     8	    read-n1 Option #Read a character
     9	    echo ""  
root@wl-ms-7673:/home/wl/desktop/shell#./test1.sh 
		Menu
1.	Display disk space
2.	Display logged on user
3.	Display memory Usage
0.	Exit menu


		Enter option:1
root@wl-ms-7673:/home/wl/Desktop/shell# 










Create a menu function



Just wrap the above code in a function.



add menu logic add corresponding case





root @ wl-MS-7673: / home / wl / desktop / 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
    28
root @ wl-MS-7673: / home / wl / desktop / shell # ./test2.sh
Menu
Display disk space
2. Display logged on user
3. Display memory usage
0. Exit menu


Enter option: 3
Exit menu
root @ wl-MS-7673: / home / wl / desktop / shell # apt-get install sed













root @ wl-MS-7673: / home / wl / desktop / 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" ;;
        *) #Input error, re-enter
        echo -e "\ nSorry, wrong selection."
        echo -en "\ n \ n \ t \ tHit any key to continue."
        read -n1 option #Reread menu options
        deal_menu ;;
        esac
    }
      
    deal_menu
root @ wl-MS-7673: / home / wl / desktop / shell #
Run the results as shown in figure:









root@wl-ms-7673:/home/wl/desktop/shell#./test3.sh 
		Menu
1.	Display disk space
2.	Display logged on user
3.	Display memory Usage
0.	Exit menu


		Enter option:















root@wl-ms-7673:/home/wl/desktop/shell#./test3.sh 
		Menu
1.	Display disk space
2.	Display logged on user
3.	Display memory Usage
0.	Exit menu


		Enter option:1
File system          1k-block used     available% mount point
/dev/sda11     68151872 7654680 57035204   12%/
udev            2039652       4  2039648    1%/dev
tmpfs            818784     908   817876    1%/run
none               5120       0     5120    0%/run/lock
None            2046956     264  2046692    1%/run/shm
root@wl-ms-7673:/home/wl/Desktop/shell# 

















using the Select command


root @ wl-MS-7673: / home / wl / desktop / shell # cat -n test4.sh
     1 #! / Bin / bash
     2       3
     4 disk_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 / desktop / shell #












The results of the run are as shown in the figure:





root @ wl-MS-7673: / home / wl / desktop / shell # ./test4.sh
1) Display disk space 3) Display memory usage
2) Display logged on user 4) Exit menu
Enter option: 1
File system 1K-block Used Available Used% Mount point
/ dev / sda11 68151872 7654632 57035252 12% /
udev 2039652 4 2039648 1% / dev
tmpfs 818784 908 817876 1% / run
none 5120 0 5120 0% / run / lock
none 2046956 264 2046692 1% / run / shm
Enter option: 2
wl pts / 0 2013-11-25 15:44 (: 0.0)
wl pts / 1 2013-11-25 17:31 (: 0.0)
Enter option: 3
MemTotal: 4093912 kB
MemFree: 2341120 kB
Buffers: 187220 kB
Cached: 702080 kB
SwapCached: 0 kB
Active: 917904 kB
Inactive: 521664 kB
Active (anon): 551016 kB
Inactive (anon): 2088 kB
Active (file): 366888 kB
Inactive (file): 519576 kB
Unevictable: 16 kB
Mlocked: 16 kB
HighTotal: 3253080 kB
HighFree: 1780332 kB
LowTotal: 840832 kB
LowFree: 560788 kB
SwapTotal: 4165628 kB
SwapFree: 4165628 kB
Dirty: 92 kB
Writeback: 0 kB
AnonPages: 550372 kB
Mapped: 218440 kB
Shmem: 2840 kB
Slab: 66112 kB
SReclaimable: 47724 kB
SUnreclaim: 18388 kB
KernelStack: 3488 kB
PageTables: 8988 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 6212584 kB
Committed_AS: 2815796 kB
VmallocTotal: 122880 kB
VmallocUsed: 30336 kB
VmallocChunk: 92168 kB
HardwareCorrupted: 0 kB
AnonHugePages: 0 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
DirectMap4k: 8184 kB
DirectMap2M: 905216 kB
Enter option: 4
Exit menu
root @ wl-MS-7673: / home / wl / desktop / shell #








using the dialog command in a script














root @ wl-MS-7673: / home / wl / desktop / 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 / desktop / shell #

The results of the operation are similar to the above.











Zenity--calendar

















root @ wl-MS-7673: / home / wl / desktop / shell # cat -n test6.sh
     1 #! / Bin / bash
     2 temp = `mktemp -t temp.XXXXXX`
     3 temp2 = `mktemp -t temp2.XXXXXX`
     4 disk_space () {
     5 df -k> $ temp
     6 zenity --text-info --title "Disk space" --filename = $ temp --width 750 --height 300
     7}
     8   
     9 whoseon () {
    10 who> $ temp
    11 zenity --text-info --title "Logged on user" --filename = $ temp --width 500 --height 200
    12}
    13
    14 menu_usage () {
    15 cat / proc / meminfo> $ temp
    16 zenity --text-info --title "Memory usage" --filename = $ temp --width 300 --height 500
    17}
    18
    19 zenity --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
    21 if [$? -Ne 1]
    22 then
    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
    35 fi
    36
    37 rm -f $ temp $ temp2 2> / dev / null
root @ wl-MS-7673: / home / wl / desktop / shell # 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.