LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-26-2009, 07:45 PM   #1
uzi85
LQ Newbie
 
Registered: Mar 2009
Posts: 3

Rep: Reputation: 0
Unhappy [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 ))
}
 
Old 03-27-2009, 01:41 AM   #2
Simon Bridge
LQ Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 198Reputation: 198
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.
 
  


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
Help with BASH script and AWK and SED NickJH Linux - Newbie 11 03-07-2009 04:08 PM
Problem changing line with sed. Should i use AWK? RattleSn@ke Linux - General 7 11-12-2007 01:03 PM
Insert character by using sed/awk manish_meet_in Linux - General 3 04-05-2007 12:19 PM
Bash Awk Variable problem _hadi_ Programming 5 12-13-2006 12:25 AM
[Bash] New line character in variable michael_hk Linux - Newbie 4 06-15-2006 02:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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