LinuxQuestions.org
Review your favorite Linux distribution.
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 11-14-2012, 06:55 AM   #1
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: San Antonio, Texas
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,495

Rep: Reputation: 85
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?
 
Old 11-14-2012, 07:33 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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).

Last edited by druuna; 11-14-2012 at 08:01 AM. Reason: Added bash man page section
 
2 members found this post helpful.
Old 11-14-2012, 07:39 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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!
 
1 members found this post helpful.
Old 11-16-2012, 03:16 PM   #4
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: San Antonio, Texas
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,495

Original Poster
Rep: Reputation: 85
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.

Last edited by fakie_flip; 11-16-2012 at 03:18 PM.
 
Old 11-17-2012, 04:26 AM   #5
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
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

Last edited by H_TeXMeX_H; 11-17-2012 at 04:28 AM.
 
Old 11-17-2012, 04:35 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@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).
 
Old 11-17-2012, 05:48 AM   #7
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Just change 'ash' to 'bash', I just know that ash is posix compliant, so that's why I wrote it as is.
 
Old 11-17-2012, 06:41 AM   #8
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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
 
Old 11-19-2012, 12:33 PM   #9
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: San Antonio, Texas
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,495

Original Poster
Rep: Reputation: 85
Quote:
Originally Posted by druuna View Post
@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.
 
Old 11-19-2012, 09:41 PM   #10
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
@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.
 
Old 11-19-2012, 10:02 PM   #11
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by fakie_flip View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash shell scripting help! computergeek7 Programming 2 04-27-2010 10:35 AM
Help with Bash shell scripting HLM01 Linux - Newbie 7 01-31-2008 04:23 PM
BASH Shell Scripting tekmann33 Linux - General 4 02-08-2007 05:03 PM
Bash shell scripting Sco Linux - Newbie 1 11-09-2004 11:58 AM
Bash Shell Scripting Help Tangerine Programming 6 05-06-2003 02:10 PM

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

All times are GMT -5. The time now is 01:38 AM.

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