LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-13-2015, 05:08 AM   #1
MrLinuxDonnelly
LQ Newbie
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 26

Rep: Reputation: Disabled
While Loop Not Working


Hey, not sure if this is the best place however thought id ask my question ..

My while loop below doesnt return as id expect :

while [[ ! "$RES" =~ (1|2|3|4|5|6|7|8|9|x|10)$ ]]
do
read -p "Please select an above option : " RES || exit
done

if [[ "$RES" =~ 1 ]]
echo "Option 1 Selected"
and so on through all my results

This goes on and works all the way up to 10.

If i select option 10 it runs option 1.

Any idea how to make sure when i select option 10 it runs it not 1.
 
Old 05-13-2015, 05:15 AM   #2
translator1111
Member
 
Registered: Jun 2010
Location: Slovakia
Distribution: Debian 8, Ubuntu 10.04 and 12.04; SLAX 6.0; ConnochaetOS 0.9.; LFS; Natty chip: VT1708S
Posts: 93
Blog Entries: 2

Rep: Reputation: 7
Dear MrLinuxDonnelly,
Quote:
This goes on and works all the way up to 10.
Is this related to the fact that the 10 is after the x?
would it work better like this?
Quote:
while [[ ! "$RES" =~ (1|2|3|4|5|6|7|8|9|x|10)$ ]]
Quote:
while [[ ! "$RES" =~ (1|2|3|4|5|6|7|8|9|10|x)$ ]]
Faithfully,
M.
 
Old 05-13-2015, 05:39 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,286

Rep: Reputation: 2347Reputation: 2347Reputation: 2347Reputation: 2347Reputation: 2347Reputation: 2347Reputation: 2347Reputation: 2347Reputation: 2347Reputation: 2347Reputation: 2347
Code:
if [[ "$RES" =~ 1$ ]]
then
    echo "match 1"
fi
if [[ "$RES" =~ 10$ ]]
then
    echo "match 10"
fi
Regexes match the target string anywhere; you need to anchor the end ($)
 
Old 05-13-2015, 05:45 AM   #4
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
How about using case?

Code:
while :; do
	read -p "Please select an above option : " res
	case "$res" in 
		1) echo "Option 1 Selected" ;;
		2) echo "Option 2 Selected" ;;
		# ...
		10) echo "Option 10 Selected" ;;
		*) break ;;
	esac
done
 
1 members found this post helpful.
Old 05-13-2015, 06:00 AM   #5
MrLinuxDonnelly
LQ Newbie
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 26

Original Poster
Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by chrism01 View Post
Code:
if [[ "$RES" =~ 1$ ]]
then
    echo "match 1"
fi
if [[ "$RES" =~ 10$ ]]
then
    echo "match 10"
fi
Regexes match the target string anywhere; you need to anchor the end ($)
Hi Chris
For some reason this didnt work for me however i found that the below while loop worked after playing around a few time
while [[ ! "$RES" =~ ^(1[0-1]|[1-9x])$ ]]

Thanks for your help though

---------- Post added 05-13-15 at 12:01 PM ----------

Quote:
Originally Posted by millgates View Post
How about using case?

Code:
while :; do
	read -p "Please select an above option : " res
	case "$res" in 
		1) echo "Option 1 Selected" ;;
		2) echo "Option 2 Selected" ;;
		# ...
		10) echo "Option 10 Selected" ;;
		*) break ;;
	esac
done
I actually slapped my head after reading this as not sure why i didnt use a case statement in the first place
 
  


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
[SOLVED] for loop is not working bkcreddy17 Programming 9 10-25-2013 08:04 AM
[SOLVED] for loop not working dth4h Linux - Software 3 05-20-2012 03:37 PM
[SOLVED] sed not working for loop say_hi_ravi Programming 3 04-05-2012 07:03 AM
how to prove working of RS-232 full modem working PC to PC and LOOP BACK getasif Programming 2 08-20-2011 09:58 AM
Bash for loop not working cryptinitedemon Linux - General 5 04-24-2010 10:44 AM


All times are GMT -5. The time now is 10:18 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration