LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   [BASH] Hangman Game: changing a character within a variable without use of sed or awk (https://www.linuxquestions.org/questions/linux-newbie-8/%5Bbash%5D-hangman-game-changing-a-character-within-a-variable-without-use-of-sed-or-awk-714711/)

uzi85 03-26-2009 07:45 PM

[BASH] Hangman Game: changing a character within a variable without use of sed or awk
 
Hi i am new to bash scripting but i am learning loads and picking it up quickly however i cant get use to syntax..
What i need to do is access a variable wich contains a word wich has been converted to dashes eg. "------". I need to compare the varaible $letter wich contains the letter the user has inputted against the variable $random_word wich contains the text format of the secret_word eg. "travel". If $letter is in $random_word then it gets the location of the letter and changes it from a "-" to $letter in the var $secret_word.

This is my code soo far however it doesnt compile please help!
(p.s I cannot use sed and awk cus tht would make it too eassy :-P)

function compare_letter () {

oldsecret_word=$secret_word
word_length=${#random_word}
i=0
while [ $i -ne $word_length ]
do
if [ "$letter"=$(echo ${random_word} | cut -c$(($i+1))) ]
then
$secret_word+=$letter
else
$secret_word+="-"
fi
done

if [ $oldsecret_word = $secret_word ]
then
$lives=$(( $lives-1 ))
echo "Bad Luck you lost a life"
else
echo "Good guess"
fi
$guess_number=$(( $guess_number+1 ))
}

Simon Bridge 03-27-2009 01:41 AM

Hint: nobody programs in a vaccuum - see how others have done it.
http://www.dbforums.com/unix-shell-s...h-hangman.html

Quote:

This is my code soo far however it doesnt compile please help!
Well it wouldn't - it's a BASH script.


All times are GMT -5. The time now is 06:03 PM.