LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Shell based menu of options (https://www.linuxquestions.org/questions/programming-9/shell-based-menu-of-options-921635/)

lucmove 01-02-2012 01:06 PM

Shell based menu of options
 
I have been testing the Fish shell for a few days, enjoying it so far to an extent. It has bugs and lacks features, but behaves pretty well in the command line, and I like it for scripts too, because the syntax is a lot simpler and cleaner.

Like I said, it lacks features. I like the 'select' command in other shells, it creates a nice menu of options. For example:

Code:

navigate ()          {
        PS3="Where do you want to go today? "
        select TARGET in google microsoft yahoo quit
        do
                case $TARGET in
                        google        )
                                local url=http://www.google.com
                                break ;;
                        microsoft        )
                                local url=http://www.microsoft.com
                                break ;;
                        yahoo        )
                                local url=http://www.yahoo.com
                                break ;;
                        quit)        exit;;
                        *)        exit ;;
                esac
        done
        lynx $url
}

Fish has no such feature so I guess I will have to use some external program. What do you recommend? I suppose things like zenity or dialog would be overkill?

kbp 01-02-2012 03:25 PM

One of the major benefits of open source is that you can modify it to suit your preference and send those modifications back upstream to possibly benefit other users, why not have a go at extending it ?

lucmove 01-02-2012 03:34 PM

Programming in shell, any shell, is hard enough for me. I am absolutely incapable of modifying the shell itself. I am in fact considering migrating 100% (scripting and command line use) to Fish because I find the other shells too complicated.

kbp 01-02-2012 03:44 PM

Programming in shell is basically an extension of working in a shell, automating common tasks etc. If you work in a shell long enough then your shell scripting skills will evolve.

David the H. 01-02-2012 06:09 PM

While I'm not familiar with fish syntax, the select command is really just a specialized while true loop. You're example function could be rewritten like this in bash, for example:

Code:

navigate (){

        echo "1) google" >&2
        echo "2) microsoft" >&2
        echo "3) yahoo" >&2
        echo "4) quit" >&2
       
        while true; do

                read -n 1 -p "Where do you want to go today (1-4)? " target

                case $TARGET in
                        1)        local url=http://www.google.com
                                break ;;

                        2)        local url=http://www.microsoft.com
                                break ;;

                        3)        local url=http://www.yahoo.com
                                break ;;

                        4)        exit ;;

                        *)        echo "Where's that?" ;;
                esac
        done

        lynx "$url"
}

I'm sure the basic idea can be adapted to almost any shell.

lucmove 01-02-2012 07:31 PM

Quote:

Originally Posted by kbp (Post 4564020)
Programming in shell is basically an extension of working in a shell, automating common tasks etc. If you work in a shell long enough then your shell scripting skills will evolve.

Sure, I have years of experience in shell programming, but I always have to make and correct at least ten mistakes until a new script runs as expected, because there are so many quirks and gotchas in Bash/sh/ksh/tcsh syntax. I am sick of (not) memorizing those. I will therefore not even touch zsh with a ten-foot pole. I remember I tried it for a few days many years ago and I got tired of it because it was too complex.

If you ever feel like reading about it, the author of the Fish shell points out exactly how Fish tries to be simpler and cleaner:

http://lwn.net/Articles/136232/

I have ported (rewritten) some 20 or 30 of my most used scripts, and they definitely look a lot more readable now. Everyday use of the command line is better, too. I make fewer mistakes.

lucmove 01-02-2012 07:35 PM

Quote:

Originally Posted by David the H. (Post 4564078)
While I'm not familiar with fish syntax, the select command is really just a specialized while true loop. You're example function could be rewritten like this in bash, for example:
(...)

That was a very helpful reply. Thank you!


All times are GMT -5. The time now is 11:38 PM.