3

My script:

echo "input yes or no"
read a
if [ $a = "yes" ] or [ $a = "Yes" ] or [ $a = "YES" ];
then
    command
else
    command
done

I have an idea that I will convert the answer (using the tr A-Z a-z command) first and after that compare with string... is that okay?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
  • 1
    See http://stackoverflow.com/questions/2264428/converting-string-to-lower-case-in-bash-shell-scripting – stalet Jun 17 '16 at 10:39
  • Dear stalet Thanks for your link I also wrote that i can convert first, but i want to know more solutions :D –  Jun 17 '16 at 10:44
  • 3
    This question have already been answered here: [Case Insensitive comparision of strings in Shell script](http://stackoverflow.com/questions/1728683/case-insensitive-comparision-of-strings-in-shell-script) – mik1904 Jun 17 '16 at 10:48
  • Check `make 1 dialog`. E.g. https://bash.cyberciti.biz/guide/A_yes/no_dialog_box – Dummy00001 Jun 17 '16 at 11:56

3 Answers3

11

Here is an example on how to do it without conversion in bash 4. You can use parameter expansion to inline change the value of the $a variable.

#!/bin/bash

echo "input yes or no"
read a
if [ ${a,,} = "yes" ];
then
    echo "test 1"
else
    echo "test 2"
fi
stalet
  • 1,345
  • 16
  • 24
8

You can use shopt -s nocasematch.

Try this :

shopt -s nocasematch
echo "Input yes or no"
read a
if [[ $a == "yes" ]]
then
    echo "YES"
else
    echo "NO"
fi

From bash:

nocasematch

If set, Bash matches patterns in a case-insensitive fashion when performing matching while executing case or [[ conditional commands.

sat
  • 14,589
  • 7
  • 46
  • 65
2

You can use the dialog tool available on most systems, which can display an interactive dialog in the console:

if dialog  --title example1 --backtitle example2 --yesno "Make a choice!" 7 60
then
    echo "YES"
else
    echo "NO"
fi

Bypasses the case-sensitivity of the user input completely.

More examples.

Dummy00001
  • 16,630
  • 5
  • 41
  • 63