LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 07-27-2006, 10:49 PM   #1
sharathkv
Member
 
Registered: Jul 2003
Distribution: HP-UX
Posts: 35

Rep: Reputation: 15
case


Hi,

In my script, I want to exit if the input is
not Y or y. I am using case:

Code:
case $res in
~[Yy])
     echo user chose to canel operation
     exit 1
     ;;
esac
How do I negate the above ~[Yy]?

I tried ~[Yy]. The script is not exiting is input is not Y or y.



Thanks
Shar
 
Old 07-27-2006, 11:04 PM   #2
crazyjimbo
Member
 
Registered: Mar 2006
Location: Edinburgh
Distribution: Debian Etch, Gentoo
Posts: 90

Rep: Reputation: 16
Instead of ~[yY], use [^yY]. The ^ tells it to match anything EXCEPT that in the bracket.
 
Old 07-27-2006, 11:21 PM   #3
sharathkv
Member
 
Registered: Jul 2003
Distribution: HP-UX
Posts: 35

Original Poster
Rep: Reputation: 15
This is not working.

Code:
echo "Enter input"
read res
case $res in
[^Yy])
     echo user chose to cancel operation.
     exit 1
     ;;
esac
echo continuing with the operation



$ ./a.ks
Enter input
!
continuing with the operation
$ ./a.ks
Enter input
m
continuing with the operation
 
Old 07-27-2006, 11:41 PM   #4
crazyjimbo
Member
 
Registered: Mar 2006
Location: Edinburgh
Distribution: Debian Etch, Gentoo
Posts: 90

Rep: Reputation: 16
I copied and pasted your script exactly and it works on my machine. Which shell are you using?

The other thing I can think of is that the script will only exit on single characters. If you enter any more than one then it will continue regardless. I'm not too hot on scripting but I'll play around and see what I can come up with.
 
Old 07-27-2006, 11:44 PM   #5
zhangmaike
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 376

Rep: Reputation: 31
If you're running this script in ksh (Korn shell) instead of sh or bash, use [!Yy] instead of [^Yy].

ksh uses a slightly different syntax for patterns.

See man ksh for more information.
 
Old 07-28-2006, 07:03 AM   #6
sharathkv
Member
 
Registered: Jul 2003
Distribution: HP-UX
Posts: 35

Original Poster
Rep: Reputation: 15
Sorry for the late reply. I was a bit busy.

Yes I am using ksh.Thanks crazyjimbo for trying.

Thanks zhangmaike,it's working now.

Bye
Shar
 
  


Reply



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
Problem With 'Case leagesell Programming 1 06-30-2006 09:32 PM
Converting sLoPPy cASE to Pretty Case with tr lowpro2k3 Programming 4 04-13-2005 08:13 PM
Why are all my upper case files being shown as lower case?? [Kernel 2.6.9-1.667 FC3] t3gah Fedora 4 03-11-2005 04:09 PM
Lower case to upper case letter sudhasmyle Programming 1 12-03-2004 04:15 AM
Case Iain Wilson Linux - Newbie 1 03-16-2004 11:51 PM

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

All times are GMT -5. The time now is 03:42 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