Find the answer to your Linux question:
Results 1 to 4 of 4
My code runs fine until I get to the end where I try to compare a user input letter with a substring. I get all kinds of results depending on ...
  1. #1
    Just Joined!
    Join Date
    Jun 2008
    Posts
    2

    Substrings

    My code runs fine until I get to the end where I try to compare a user input letter with a substring. I get all kinds of results depending on the letter I guess. Have I made an error in my code?

    Code:
    #!/bin/bash
    
    printf "Please type a word : "
    read word
    
    printf "guess a letter : "
    read guess
    
    for (( i=0; i<${#word}; i++ ))
    do
    if (( $guess == ${word:$i:1} ))
    then
    printf "yes"
    fi
    done

  2. #2
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    hi,


    replace
    Code:
    if (( $guess == ${word:$i:1} ))
    with

    Code:
    if [ $guess == ${word:$i:1} ]
    Linux and me it's a love story

  3. #3
    Just Joined!
    Join Date
    Jun 2008
    Posts
    2
    Worked. Cheers! Should I do the same with "for" and "if" ?

  4. #4
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Yes, unless you prefer typing 4 shifted characters instead of two unshifted ones (programmers just don't seem to be as lazy as we used be!).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...