Find the answer to your Linux question:
Results 1 to 9 of 9
Hi, I'm trying to get a dropdown selection menu in a bash script, like the one GRUB presents you with, allowing the user to peruse a playlist and select a ...
  1. #1
    Just Joined! chochem's Avatar
    Join Date
    Feb 2008
    Posts
    7

    Dropdown menu (GRUB-style) in BASH script

    Hi,

    I'm trying to get a dropdown selection menu in a bash script, like the one GRUB presents you with, allowing the user to peruse a playlist and select a specific track. I've tried mucking about with a while loop that prints the whole list and a read -p -n 1 that watches the arrow keys for input but since I have to clear the screen and reprint the whole thing each time there's user input, it only works well for small lists. the 'dialog' utility seems like it might be an option but I can'get it to accept a dynamically created list (by a while loop). Isn't there some built-in bash selector I can use?

  2. #2
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by chochem View Post
    Hi,

    I'm trying to get a dropdown selection menu in a bash script, like the one GRUB presents you with, allowing the user to peruse a playlist and select a specific track. I've tried mucking about with a while loop that prints the whole list and a read -p -n 1 that watches the arrow keys for input but since I have to clear the screen and reprint the whole thing each time there's user input, it only works well for small lists. the 'dialog' utility seems like it might be an option but I can'get it to accept a dynamically created list (by a while loop). Isn't there some built-in bash selector I can use?
    I will better shot myself in one foot before using bash for this. If you really want to use bash for any reason, I would just print a number in front of each track, match them using an array or something, and use read to read the track number and play it.

    What you are trying to do in bash is a real overkill. I'd suggest using dialog or ncurses, but not under bash. If you don't want to compile, maybe python would be ok for you.

  3. #3
    Just Joined! chochem's Avatar
    Join Date
    Feb 2008
    Posts
    7
    Well, that's a tad drastic Well, you may be right, but given what I'm still learning about bash I'm pretty sure that I've only scraped the surface and I like the 'works anywhere, anytime' part.

    At any rate, learning ncurses and python would seem to require a bit too much investment for such a humble task. And even if I could get dialog to work, it's so ******* ugly....

  4. #4
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by chochem View Post
    Well, that's a tad drastic Well, you may be right, but given what I'm still learning about bash I'm pretty sure that I've only scraped the surface and I like the 'works anywhere, anytime' part.

    At any rate, learning ncurses and python would seem to require a bit too much investment for such a humble task. And even if I could get dialog to work, it's so ******* ugly....
    Don't misunderstand me: bash is an awesome shell, but it has not been designed with that kind of purposes in mind. I really haven't dealt with the dialog userland tools, they might be any good or not, I don't really know, but that's not bash really. The same that mangling strings on awk is not bash either... That's shell independant.

    If dialog worked for you, then that's fine. But if you are really worried about portability, nor even bash would be good enough. In that case, you would be bound to sh, which is much more limited even.

  5. #5
    Just Joined!
    Join Date
    Dec 2006
    Posts
    52
    How about select/case combination. That presents a numbered list to choose from.

    select i in `ls` Exit
    do
    case ${i} in
    ${i})
    Do something here
    ;;
    Exit)
    break
    ;;
    *)
    Unnecessary error
    ;;
    esac
    done

  6. #6
    Just Joined! chochem's Avatar
    Join Date
    Feb 2008
    Posts
    7
    Quote Originally Posted by ennoil View Post
    How about select/case combination. That presents a numbered list to choose from.

    select i in `ls` Exit
    do
    case ${i} in
    ${i})
    Do something here
    ;;
    Exit)
    break
    ;;
    *)
    Unnecessary error
    ;;
    esac
    done
    Thanks for this. It's not really what I wanted but given the constraints I've set, it's probably the best solution.

  7. #7
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    Quote Originally Posted by chochem
    ... And even if I could get dialog to work, it's so ******* ugly ...
    Have you looked at zenity? I didn't notice a drop-down list option, but there are a lot of features, and perhaps one of them would be suitable ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  8. #8
    Just Joined! chochem's Avatar
    Join Date
    Feb 2008
    Posts
    7
    I think I have come across it but it's GTK and I'm looking at a pure text interface solution. But thanks, anyway.

  9. #9
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.
    Quote Originally Posted by chochem View Post
    ... I'm looking at a pure text interface solution.
    There is the non-trivial twin, with 10-20 external clients, apparently callable from the shell:
    Twin is a text-mode windowing environment: it draws and manages text windows on a text-mode display, like X11 does for graphical windows. It has a built-in window manager and terminal emulator, and can be used as server for remote clients in the same style as X11. It can display on Linux console, on X11 and inside itself.

    ...

    a. Each window has an associated menu. Many windows can share
    the same menu, and twin always show the menu associated
    to the currently focused window.

    d. Twin implements virtual screens. Each virtual screen has very big
    sizes in both directions (something like 64K character cells),
    and you can scroll them by holding LEFT or MIDDLE mouse buttons
    and moving the mouse to one of the screen borders. Also, you can
    switch to the next virtual screen by clicking on the arrows
    at the top-right of its menu bar.

    -- Twin - textmode window environment for Linux
    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

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