Results 1 to 4 of 4

Thread: Creating menu options in Bash?

  1. #1
    Join Date
    Aug 2009
    Beans
    67
    Distro
    Ubuntu

    Question Creating menu options in Bash?

    Hello, I am the author of the program GISAH that's written in DOS Batch for Windows. My skills with Bash aren't as great as they are with Batch. I wanted to make a Bash version of GISAH for use on GNU/Linux, BSD, etc but one thing is holding me up: Menu choices

    In Batch, I used set to forge these, like so:

    @echo off

    echo I. Analyze all volumes.
    echo II. Defrag Windows Drive
    echo A. Main Menu

    set choice=
    set /p choice=

    if not '%choice%'=='' set choice=%choice:~0,1%
    if '%choice%'=='1' goto :analyze
    if '%choice%'=='2' GOTO :defrag
    if '%choice%'=='a' GOTO :return
    if '%choice%'=='A' GOTO :return


    See, I would first issue the @echo off to prevent the input lines from appearing when ran in the MS-DOS shell. Then use echo to print the available options. Then, in order to make those options function as such, use "set" to create the "choice" variable. I tried simply copying and pasting this into a .sh file and such but I got an error when I ran it in bash.

    I've heard of many other commands such as select and case. But they confuse me. Thanks all

  2. #2
    Join Date
    Dec 2010
    Location
    Earth Planet
    Beans
    162
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Creating menu options in Bash?

    Welcome to the world of bash programming
    Here is an example trying to do what you explained:

    Code:
    #!/bin/bash
    
    function analyze_volumes {
        echo "analyze volume function";
    }
    
    function defrag {
        echo "defrag function";
    }
    
    function main_menu {
        echo "main menu function";
    }
    
    select choice in \
        "Analyze all volumes" \
        "Defrag Windows Drive" \
        "Main Menu" \
        "Exit"
    do
        case $choice in
            "Analyze all volumes")
                analyze_volumes;
                ;;
            "Defrag Windows Drive")
                defrag;
                ;;
            "Main Menu")
                main_menu;
                ;;
            "Exit")
                break;
                ;;
            *)
                echo "Please select an option";
                ;;
        esac
    done
    echo "Finish!"
    You can define functions instead of plain use of goto calls.
    You can use "select" with a list of space separated words to select an option.
    You can use "case" to match against patterns.

  3. #3
    Join Date
    Aug 2009
    Beans
    67
    Distro
    Ubuntu

    Re: Creating menu options in Bash?

    Very cool! I especially liked the sharp there at the end (#). Now I must figure out how to connect multiple scripts. Thanks! Appreciate it.

  4. #4
    Join Date
    Aug 2009
    Beans
    67
    Distro
    Ubuntu

    Re: Creating menu options in Bash?

    It also seems like I have more possibilities with those function commands. Me likey.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •