LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Bash shell scripting (http://www.linuxquestions.org/questions/programming-9/bash-shell-scripting-4175437096/)

fakie_flip 11-14-2012 07: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 08:33 AM

Have a look at this:
Code:

#!/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).

colucix 11-14-2012 08:39 AM

Maybe using something like this:
Code:

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

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

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

fakie_flip 11-16-2012 04:16 PM

The Linux+ 2nd examination certification objectives says:

Quote:

• 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 05:26 AM

This is posix compliant:

Code:

#!/bin/ash

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

case "$ans" in
        y|Y|yes|Yes)
                echo 'Yes entered'
        ;;
        n|N|no|No)
                echo 'No entered'
        ;;
        *)
                echo 'Input error'
                exit 1
        ;;
esac

exit 0


druuna 11-17-2012 05: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:
Quote:

--posix
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 06: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 07: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.

Code:

case "$ANSWER" in
y|Y|[yY][eE][sS])
    # do something
    ;;
n|N|[nN][oO])
    # do something
    ;;
*)
    # invalid answer
    ;;
esac


fakie_flip 11-19-2012 01:33 PM

Quote:

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.

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.

konsolebox 11-19-2012 10: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 11:02 PM

Quote:

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 03:08 PM.