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 03-27-2009, 11:21 AM   #1
uzi85
LQ Newbie
 
Registered: Mar 2009
Posts: 3

Rep: Reputation: 0
BASH hangman: loop which adds the character to the variable if it matches


I can't seem to get this loop to output the secret_word variable with the correctly guessed letter replaced within the variable, this is my code can sum please tell me where i am goin worng..

function compare_letter () {
oldsecret_word=$secret_word
word_length=${#random_word}
secret_word=""
i=0
while [ $i -eq $word_length ]
do
if [ "$letter"=$(echo $random_word | cut -c$(($i+1))) ]
then
$secret_word+="$letter"
else
$secret_word+="-"
fi
done
}
 
Old 03-27-2009, 12:35 PM   #2
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,775

Rep: Reputation: 481Reputation: 481Reputation: 481Reputation: 481Reputation: 481
Please enclose your code insdie CODE tags.
This:
$secret_word+="$letter"
should be:
secret_word+="$letter"
Unless you are trying to add "$letter" to secret_word each time. In that case you'd need something like:
secret_word="$secret_word$letter"

Be sure to post your game when you finish.
 
Old 03-28-2009, 06:23 AM   #3
uzi85
LQ Newbie
 
Registered: Mar 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Ah your rite thanks alot for the help what stupid newbie syntax error to make... i shall post the game when i have finished.
 
Old 03-28-2009, 10:31 AM   #4
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,775

Rep: Reputation: 481Reputation: 481Reputation: 481Reputation: 481Reputation: 481
The reason I asked you to post it is that I am nuts about all things 'bash' -so it will be nice to add another bash toy to my collection.
 
  


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
[BASH] Hangman Game: changing a character within a variable without use of sed or awk uzi85 Linux - Newbie 1 03-27-2009 02:41 AM
bash - loop over variable array names talanis Programming 2 02-19-2009 12:09 PM
Bash: Counting the number of character occurences in a variable basildon Linux - Newbie 3 09-22-2008 11:11 AM
[Bash] New line character in variable michael_hk Linux - Newbie 4 06-15-2006 03:13 PM
bash: routine outputting both matches and non-matches separately??? Bebo Programming 8 07-19-2004 07:52 AM


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