-   Programming (
-   -   Bash shell scripting (

fakie_flip 11-14-2012 06:55 AM

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?

druuna 11-14-2012 07:33 AM

Have a look at this:


echo -n "Are you sure : "

read ans

case "${ans,,}" in
  y|yes ) echo "Yes." ;;
  n|no ) echo "No." ;;
  *) echo "oops..." ;;

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).

colucix 11-14-2012 07:39 AM

Maybe using something like this:

read -p "Are you a Klingon? [y/n] " ans

if [[ $ans =~ [Yy][Ee]*[Ss]* ]]
  do something
  do something else

This permits also answers like yes, yeeees, yesssss, yeeessss and so on! ;)

fakie_flip 11-16-2012 03:16 PM

The Linux+ 2nd examination certification objectives says:


• Use standard sh syntax (loops, tests).
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.

H_TeXMeX_H 11-17-2012 04:26 AM

This is posix compliant:



read -p 'Are you sure ? [y/n] ' ans

case "$ans" in
                echo 'Yes entered'
                echo 'No entered'
                echo 'Input error'
                exit 1

exit 0

druuna 11-17-2012 04:35 AM

@H_TeXMeX_H: I do believe bash needs to be used (have a look at the LPI link).

Besides from that; What exactly determines (bash) POSIX compliance?

If I include set -o posix and run my solution (post #2) it works.

This about set -o posix / --posix from the bash manual page:

Change the behavior of bash where the default operation differs
from the POSIX standard to match the standard (posix mode).

H_TeXMeX_H 11-17-2012 05:48 AM

Just change 'ash' to 'bash', I just know that ash is posix compliant, so that's why I wrote it as is.

konsolebox 11-17-2012 06:41 AM

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 11-19-2012 12:33 PM


Originally Posted by druuna (Post 4831277)
@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.


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.

konsolebox 11-19-2012 09:41 PM

@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.

konsolebox 11-19-2012 10:02 PM


Originally Posted by fakie_flip (Post 4832730)
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.

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