ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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 ?
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.
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.
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.
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:
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.
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:
(...)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.