ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Distribution: Gentoo Hardened using OpenRC not Systemd
Bash shell scripting
I need to ask the user a yes or no question. I want to check the answer without being case sensitive. The user might enter y, n, yes, or no, not counting case. How is this typically handled in a bash or sh script? I was thinking of taking the first character of the users input, converting it to lower case and checking for y or n. How can I do this in sh or bash?
I like the bash extensions to POSIX sh, but I must do well on this exam! Sorry I didn't make that clear earlier, but I am still confused. The exam seems to want some scripting in bash but also demands standard sh syntax.
Last edited by fakie_flip; 11-16-2012 at 03:18 PM.
I think this format should be good enough and is compatible with all shells based from the original bourne shell with strict compatibility to it which could include ash, dash, ksh, pdksh and zsh not to mention bash. Note that this is not based from POSIX and should work even though POSIX didn't exist.
case "$ANSWER" in
# do something
# do something
# invalid answer
@fakie_flip: I hope you considered the example I gave you which doesn't really care if you're running in bash or not, or in POSIX mode or not.
Anyway you actually have at least four choices when writing a script.
a) You use bash and pick a minimum version of it for its features like 3.0 or 4.0.
b) You follow a syntax that's actually compatible with all shells moreover based from the original shell. So you actually test if your script would work in most shells, or at least all shells that should work the way they should if they strictly base themselves from the original shell; as some shells don't follow well. Note that the POSIX standard is different from this and it (POSIX) was created later to have unity among shells.
c) Follow the POSIX standard and hope all shells that supports it have the feature to follow it properly.
d) Use other shells.
It's not always clear whether I should look up the bash way of doing it or the standard sh POSIX way doing it.
I think knowing or learning the difference with being compatible to most shells instead of having the good features of one shell would greatly help for that. There's just so many things you can't do with compatibility like arrays and process substitution. On the other hand if it's really meant to just run on many shells then I guess you have no choice but be compatible suffering limited and difficult features. There's a solution like universability through shell-specific function selection and/or encapsulation of capabilities through functions, but it's never easy and is not always applicable to most tasks.