LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-10-2008, 03:47 PM   #1
malikah
LQ Newbie
 
Registered: Jun 2006
Posts: 12

Rep: Reputation: 0
if statement


Is there a mistake in the following code?

printf "${wrdCpy[$a]}" ##Prints the letter "A"
printf "$guess" ##Prints the letter "A"

if [ "$guess" == "${wrdCpy[$a]}" ]
##Tries to tell me that "A" is not equal to "A"..
 
Old 07-10-2008, 09:59 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Works for me, note that if you don't put a space around the brackets or == it won't work right.

Code:
~/tmp$ wrdCpy=(A B C)
~/tmp$ guess=A
~/tmp$ a=0
~/tmp$ printf "${wrdCpy[$a]}" ##Prints the letter "A"
A~/tmp$ printf "$guess" ##Prints the letter "A"
A~/tmp$ if [ "$guess" == "${wrdCpy[$a]}" ] ; then echo "A equals A" ; else echo "A doesn't equal A" ; fi
A equals A
Also I think it should be [ "$guess" = "${wrdCpy[$a]}" ], although it seems to work fine with ==.
 
Old 07-10-2008, 10:06 PM   #3
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Actually, I just learned something new today regarding "=" and "==".

Bash allows either, with the single = being POSIX compliant, but the preferred being double equals.
man test shows only a single = as being correct.

So, the behavior depends on the shell and which test is used (built-in v. /usr/bin/test command, which is likely rarely used today).
 
  


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
Case statement with If statement cbo0485 Linux - Newbie 4 11-07-2007 08:05 PM
BASH IF statement kinetik Programming 10 05-07-2006 02:48 AM
the FOR statement (C) sk84blood813 Programming 3 12-09-2004 08:25 AM
For Statement bendeco13 Linux - General 6 10-31-2004 05:31 PM
If Statement junjem0702 Programming 4 08-23-2004 09:49 AM

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

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