LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 03-27-2009, 10: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, 11:35 AM   #2
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
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, 05: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, 09:31 AM   #4
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
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



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 01:41 AM
bash - loop over variable array names talanis Programming 2 02-19-2009 11:09 AM
Bash: Counting the number of character occurences in a variable basildon Linux - Newbie 3 09-22-2008 10:11 AM
[Bash] New line character in variable michael_hk Linux - Newbie 4 06-15-2006 02:13 PM
bash: routine outputting both matches and non-matches separately??? Bebo Programming 8 07-19-2004 06:52 AM

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

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