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.
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,495
Rep:
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?
#!/bin/bash
echo -n "Are you sure : "
read ans
case "${ans,,}" in
y|yes ) echo "Yes." ;;
n|no ) echo "No." ;;
*) echo "oops..." ;;
esac
This ${ans,,} is a bash internal that changes all to lower case (${ans^^} to make upper case). See the bash man page for details (Parameter Expansion section).
Last edited by druuna; 11-14-2012 at 08:01 AM.
Reason: Added bash man page section
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.
Code:
case "$ANSWER" in
y|Y|[yY][eE][sS])
# do something
;;
n|N|[nN][oO])
# do something
;;
*)
# invalid answer
;;
esac
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,495
Original Poster
Rep:
Quote:
Originally Posted by druuna
@H_TeXMeX_H: I do believe bash needs to be used (have a look at the LPI link).
Bash is used in some places and standard sh syntax POSIX compliant in others. I know it's confusing. I posted about this earlier.
Quote:
• Use standard sh syntax (loops, tests).
I think this would fall under the test category.
However when I'm writing scripts for practice, and I don't know how to do something. It's not always clear whether I should look up the bash way of doing it or the standard sh POSIX way doing it.
@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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.