PDA

View Full Version : bash replacing characters within a variable


baks
March 15th, 2007, 02:36
Hi, can someone please help me PLEASE ?

I have a variable set as;
Word_used=bakar
The variable was then changed to a variable called $hidden “-----“ using the tr command.

My program accepts a single character entered by the user, checks it to see if exists in the word_used variable.

If the character exists in the word_used variable then the program should input that letter into the hidden word. For example if the letter k was entered by the user, the program should print --k--

i cannot get the program to enter the letter into the word if is correct.

The following is my code:

while [ $lives -ge 1 ]; do
counter1=1
echo
echo $hiddenword
echo "please enter a letter "
read input_letter # accepts the input
clear

if [ -z $input_letter ]; then # checks if nothing has been entered
echo "you have not entered anything"
echo "please enter a letter"
validation3=0 # resets loop
else

input_letter_length=`expr length $input_letter` # get length of input letter
if [ $input_letter_length -eq 1 ]; then

while [ $counter1 -le $word_length ];do # checks the letter input against the word_used
character=`echo $word_used | cut -b$counter1` # check each one at a time

if [ $input_letter = $character ];then



# THIS IS WHERE THE PROGRAM SHOULD ENTER THE LETTER INPUT BY THE USER INTO THE VARIABLE $HIDDENWORD. I NEED HELP ON HOW TO DO THIS.


fi
counter1=`expr $counter1 + 1` #continues loop until counter = length of word
done
# prints out the results for each letter
echo $hidden_word # prints out the hidden word with the correct letters
echo "you have entered the following letters $list_of_answers"
lives=`expr $lives - 1`
number_of_tries=`expr length $list_of_answers`
echo "you have had $number_of_tries tries"
echo "you have another $lives lives"
validation3=1 # this loop will contiue until lives has run out or incorrect data is entered
fi
fi

baks
March 16th, 2007, 05:18
Ignore the above post Ive managed it.