LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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-14-2007, 07:26 PM   #1
baks
Member
 
Registered: Feb 2007
Posts: 41

Rep: Reputation: 15
Unhappy bash replacing characters within a variable


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
 
Old 03-15-2007, 10:15 AM   #2
baks
Member
 
Registered: Feb 2007
Posts: 41

Original Poster
Rep: Reputation: 15
Can anyone help ?
 
Old 03-15-2007, 12:16 PM   #3
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
In order to facilitate commentary, I have added line #'s to your code & put it in "Code" block below:
Code:
 1: while [ $lives -ge 1 ]; do             
 2:    counter1=1
 3:    echo
 4:    echo $hiddenword
 5:    echo "please enter a letter "
 6:    read input_letter                                        
 7:    # accepts the input
 8:    clear                                                    
 9:    
10:    # checks if nothing has been entered
11:    if [ -z $input_letter ]; then                             
12:       echo "you have not entered anything"                  
13:       echo "please enter a letter"
14:       validation3=0                                         
15:       # resets loop
16:    else                                   
17:       input_letter_length=`expr length $input_letter`       
18:       # get length of input letter
19:       if [ $input_letter_length -eq 1 ]; then
20: 
21:          # checks the letter input against the word_used
22:          while [ $counter1 -le $word_length ]; do  
23:             character=`echo $word_used | cut -b$counter1`   
24:             # check each one at a time
25:             if [ $input_letter = $character ]; then 
26: 
27:      # THIS IS WHERE THE PROGRAM SHOULD ENTER THE LETTER
28:      #  INPUT BY THE USER INTO THE VARIABLE $HIDDENWORD. 
29:      # I NEED HELP ON HOW TO DO THIS. 
30: 
31:             fi
32:             counter1=`expr $counter1 + 1`    
33:             #continues loop until counter = length of word
34:          done
35:          # prints out the results for each letter
36:          echo $hidden_word           
37:          # prints out the hidden word with the correct letters 
38:          echo "you have entered the following letters "   \
                  "$list_of_answers"
39:          lives=`expr $lives - 1`
40:          number_of_tries=`expr length $list_of_answers`
41:          echo "you have had $number_of_tries tries"
42:          echo "you have another $lives lives"
43:          validation3=1  
44:          #   this loop will contiue until lives has run out 
45:          #   or incorrect data is entered
46:       fi
47:    fi
48: done
In order to minimize horizontal scrolling in either of my browsers (Konqueror 3.3.2 & Opera 9); I put your original comments on the following line, changed the indents to 3 spaces, & broke l. 38 w/ a '\'.

I also added the missing closing "done" command.

I am preparing a long, & I hope helpful, reply.

Last edited by archtoad6; 03-15-2007 at 12:27 PM.
 
Old 03-15-2007, 07:32 PM   #4
baks
Member
 
Registered: Feb 2007
Posts: 41

Original Poster
Rep: Reputation: 15
Thank you very much.
 
Old 03-15-2007, 08:39 PM   #5
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by baks
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

You haven't initialized $lives.
Quote:
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

POSIX shells (bash, ksh, ash, etc.) have a parameter expansion to get the length of a variable:
Code:
input_letter_length=${#input_letter}
Quote:
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

You don't need a counter; use temporary variables, extracting and removing the first character each time through the loop:
Code:
  word_right=$used_word
  hidden_right=$hidden
  hidden=
  while :
  do
    [ -z "$word_right" ] && break 
    right_temp=${word_right#?}
    word_letter=${word_right%"$right_temp"}
    hidden_temp=${hidden_right#?}
    hidden_letter=${hidden_right%"$hidden_temp"}
    if [ "$word_letter" = "$input_letter" ]
    then
      hidden_letter=$input_letter
    fi
    hidden=$hidden$hidden_letter
    word_right=$right_temp
    hidden_right=$hidden_temp
  done
Quote:
# 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`

In a POSIX shell, you do not need expr; the shell can do arithmetic:
Code:
lives=$(( $lives - 1 ))
Quote:
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

Last edited by cfaj; 03-15-2007 at 08:40 PM.
 
Old 03-19-2007, 12:33 AM   #6
baks
Member
 
Registered: Feb 2007
Posts: 41

Original Poster
Rep: Reputation: 15
Thank You greatly appreciated.
 
  


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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
Replacing characters in file names? yuccabrevifolia Linux - Software 1 04-19-2006 01:31 PM
bash history - replacing arguments Vrajgh Linux - Software 4 03-08-2006 02:36 AM
replacing characters in a file with others pcdude Linux - General 6 01-19-2005 12:29 AM


All times are GMT -5. The time now is 11:42 PM.

Main Menu
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