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

Rep: Reputation: 0
double check


Ok, I've spent an hour researching this one, I deserve to ask. I'm trying to make sure that the player enters letters(lower case) only AND that the word is in the dictionary. What am I doing wrong here:

Code:
printf "Player one, enter a word: ";read word

while [ "$(echo $word | egrep '[^a-z]')" ] || [ "$( egrep -v "^$word" /usr/share/dict/words)" ];do

printf "Word must contain lower-case letters only and be in the dictionary.. Try again: ";read word

done
 
Old 07-13-2008, 07:10 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
When checking the condition, you are using the $() construct, which expands to the output of the command line inside the brackets.

Code:
~/tmp$ egrep -v IamNOTaWORD /usr/dict/words | wc -l
  25143
So when your grep expands to all the words in the dictionary! This output is passed as the arguments to '[' (aka test), which follows this rule:
Code:
 ~/tmp$ help test
...
        STRING         True if string is not empty.
...
Since the grep will give output for any string you give, the second condition is always true, and your loop never ends.

Here's something that seems to work
Code:
#!/bin/bash
read -p"Player one, enter a word: " word
while echo $word | egrep '[^a-z]' >/dev/null || ! egrep "^$word" /usr/dict/words >/dev/null; do
    read -p"Word must contain lower-case letters only and be in the dictionary.. Try again: " word
done
Note that I've used the exit status of the grep commands rather than '[' on their output. The ">/dev/null" makes sure no output goes to the screen. Also an egrep -v with a non-word returns success because it finds lines that don't match the given pattern, so I dropped the -v and use ! to negate the return status.
 
  


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
can someone double check these computer components for me please Nonc Linux - Hardware 5 01-10-2008 07:48 PM
someone double check my xorg.conf plz? (tri-monitor woes ubuntu 5.10) 0okami Linux - Hardware 4 01-04-2006 10:40 AM
Wanted to double check before XP install dualboot degraffenried13 Linux - General 2 06-05-2005 11:55 AM
Could someone double check my plan? rayber2000 Linux - Networking 2 03-02-2005 08:20 AM
Partition check, check double check Vincentius Linux - General 0 12-25-2004 05:47 AM

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

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