LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 10-27-2009, 04:06 AM   #1
rahulruns
LQ Newbie
 
Registered: Feb 2008
Posts: 21

Rep: Reputation: 0
Comparing Multiple Values in while loop


How do multiple values could be comapred in a while loop in bash. The values are numerical not string

while [[ $ci_ty -ne 1 || $ci_ty -ne 2 || $ci_ty -ne 3 ]]
do
echo "enter valid choice"
read $ci_ty
done

The above script gives error

line 233: read: `8': not a valid identifier
 
Old 10-27-2009, 04:11 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
try it another way using infinity loop
Code:
while true
do
   if [ "$cy_ty" -ne 1 -o "$cy_ty" -ne "2" ..... ];then
        #echo ....
   else 
      break
   fi
done
 
Old 10-27-2009, 04:14 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

this: read $ci_ty

should be: read ci_ty (no $).

Hope this helps.
 
Old 10-27-2009, 05:05 AM   #4
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 101Reputation: 101
Quote:
Originally Posted by rahulruns View Post
How do multiple values could be comapred in a while loop in bash. The values are numerical not string

while [[ $ci_ty -ne 1 || $ci_ty -ne 2 || $ci_ty -ne 3 ]]
do
echo "enter valid choice"
read $ci_ty
done

The above script gives error

line 233: read: `8': not a valid identifier
Code:
while [[ \( $ci_ty -ne 1 \) -o \( $ci_ty -ne 2 \) -o \( $ci_ty -ne 3 \) ]]
do
    echo "enter valid choice"
    read $ci_ty
done
The escapes I have added may not be necessary. You cold have found all this out with:

Code:
$ man test
Oh -- I just noticed something -- your logic is backwards. You actually want:

Code:
while [[ \( $ci_ty -ne 1 \) -a \( $ci_ty -ne 2 \) -a \( $ci_ty -ne 3 \) ]]
do
    echo "enter valid choice"
    read $ci_ty
done
 
Old 10-27-2009, 05:39 AM   #5
rahulruns
LQ Newbie
 
Registered: Feb 2008
Posts: 21

Original Poster
Rep: Reputation: 0
Rectified the error $ci_ty was the issue. Thank you for your replies
 
Old 10-27-2009, 05:59 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
@lutusp: You also need to remove the $ from the read $ci_tv in your example code snippets.

This was a read problem from the start as shown in the error: line 233: read: `8': not a valid identifier.

And yes I know the code snippet provided by the OP has other issues, but I'm not going to assume anything. Just here to solve the initial error.
 
  


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
matching multiple values in awk vgr12386 Programming 3 06-15-2009 03:54 AM
libnet packet construction - wrong values = infinite loop?! westies870 Programming 0 05-13-2008 05:02 PM
Enter multiple values and display them all at once ckoniecny Programming 0 10-17-2006 03:57 PM
xmodmap for multiple values malo_umoran Slackware 3 03-27-2005 09:39 AM
bash - comparing a variable to several values davee Programming 3 05-05-2003 07:26 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration