LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-21-2016, 06:31 AM   #1
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311
Blog Entries: 2

Rep: Reputation: 16
Angry conditional statements: Forgot how to read both capital/ lower case


Before I am sure that I saw some tutorial that taught that you can write a program with a if/else conditional statements that can accept the following answers:
Quote:
1) Yes
2) YEs
3) YES
4) yes
5) yES
6) yeS
7) YeS
8) yEs
My failed attempt below:
Code:
#!/bin/bash

unset ans

echo -en "Enter into call mode? > "  
read ans

#Conditional statement
	if [ "$ans" == "[Yy][Ee][Ss] ]; then
		echo "You answered "$ans", Welcome!"
	
	elif [ "$ans" == "[Nn][Oo]" ]; then
		echo "You answered "$ans", Denied"
	else
		echo "Input not valid."
	fi
There must be something wrong with my web searching skills or something not to be able to find this again. The answer stems off of [0-9a-zA-Z] being shorthand for numbers 0-9, lower case letters a-z and upper case letters A-Z. I even searched in the ebooks: Bash Beginners Guide and Advanced Bash Scripting.

Last edited by andrew.comly; 06-21-2016 at 06:33 AM. Reason: correction
 
Old 06-21-2016, 06:59 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
see man bash,
Code:
[[ "$ans" =~ [Yy][Ee][Ss] ]]
1. must use [[ and ]] instead of [ ]
2. must not use " " around the regexp
3. =~ is the comparator, not ==
 
1 members found this post helpful.
Old 06-21-2016, 07:07 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Too late... What you are searching for is conditionals using regular expressions.

[[ <string> =~ <regex> ]]

Last edited by michaelk; 06-21-2016 at 07:09 AM.
 
1 members found this post helpful.
Old 06-21-2016, 07:53 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
Generally speaking, languages are case-sensitive in string comparison. (But, as always, there are exceptions.) Regular expressions (with the "/i" modifier) can reliably be used to do case-insensitive compares. Or, you can use a "to-upper/lower case" function and compare against a literal of that case.
 
Old 06-21-2016, 08:12 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I use bashes butilin ^^ or ,, to shift entire string to upper or lower case and then do basic string test:
Code:
[[ ${ans^^} == "YES" ]]
 
2 members found this post helpful.
Old 06-21-2016, 11:38 AM   #6
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
[ "$ans" == "[Yy][Ee][Ss] ]
also missing closing double quote
 
1 members found this post helpful.
Old 06-21-2016, 12:11 PM   #7
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Another way is to use shopt -s nocasematch
Code:
shopt -s nocasematch
if [[ $ans == "yes" ]]; then
    echo "You answered '$ans', Welcome!"
	
  elif [[ $ans == "no" ]]; then
    echo "You answered '$ans', Denied"
  else
    echo "Input not valid."
  fi
 
2 members found this post helpful.
Old 06-21-2016, 12:13 PM   #8
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Q

Quote:
Originally Posted by keefaz View Post
Another way is to use shopt -s nocasematch
Code:
shopt -s nocasematch
if [[ $ans == "yes" ]]; then
    echo "You answered '$ans', Welcome!"
	
  elif [[ $ans == "no" ]]; then
    echo "You answered '$ans', Denied"
  else
    echo "Input not valid."
  fi
Good Stuff, Maynard!
Thanks!
 
1 members found this post helpful.
Old 06-21-2016, 10:41 PM   #9
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Talking I found it!

It WAS in Advanced Bash Scripting's Chapter 18.1. "A Brief Introduction to Regular Expressions" online eBook under the "Brackets -- [...] -- enclose a set of characters to match in a single RE." section in the sentence beginning with "Combined sequences".

I originally looked in the wrong chapter!! More specifically:
Chapter 9. Another Look at Variables...
Chapter 10. Manipulating Variables.
 
Old 06-22-2016, 12:00 AM   #10
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Talking Solved, Much thanks!

Special thanks to pan64, grail and keefaz for your solutions!!

Synopsis:
Quote:
Originally Posted by pan64 View Post
Code:
[[ "$ans" =~ [Yy][Ee][Ss] ]]
1. must use [[ and ]] instead of [ ]
2. must not use " " around the regexp
3. =~ is the comparator, not ==
Quote:
Originally Posted by grail View Post
Code:
[[ ${ans^^} == "YES" ]]
Quote:
Originally Posted by keefaz View Post
Code:
shopt -s nocasematch
if [[ $ans == "yes" ]]; then
    echo "You answered '$ans', Welcome!"
	
  elif [[ $ans == "no" ]]; then
    echo "You answered '$ans', Denied"
  else
    echo "Input not valid."
  fi
 
  


Reply

Tags
case-insensitive, characters, condition, read, test



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Error in Conditional Statements newuser85 Linux - Newbie 9 09-11-2015 08:09 AM
[SOLVED] Question about conditional statements. jason13131414 Linux - Newbie 4 04-05-2013 04:18 PM
Cannot get conditional statements to work in xDialog/Dialog Stevithen Programming 7 02-05-2011 06:03 PM
if statements and case statements not working in bourne shell script mparkhurs Programming 3 06-12-2004 02:41 AM
conditional statements in gmake on Linux malik Linux - Software 0 04-28-2004 10:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration