Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux Hosts
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Linux Programming & Scripting
Reload this Page Substrings
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Linux Programming & Scripting C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Reply
 
Thread Tools Display Modes
Old 06-16-2008   #1 (permalink)
gatzke
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
gatzke is offline   Reply With Quote
Old 06-16-2008   #2 (permalink)
khafa
Linux Enthusiast
 
khafa's Avatar
 
Join Date: Apr 2008
Location: Tokyo, Japan
Posts: 719
hi,


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

Code:
if [ $guess == ${word:$i:1} ]
__________________
Linux and me it's a love story
khafa is offline   Reply With Quote
Old 06-16-2008   #3 (permalink)
gatzke
Just Joined!
 
Join Date: Jun 2008
Posts: 2
Worked. Cheers! Should I do the same with "for" and "if" ?
gatzke is offline   Reply With Quote
Old 06-18-2008   #4 (permalink)
scm
Linux Engineer
 
Join Date: Feb 2005
Posts: 982
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!).
scm is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 11:20 AM.




© 2000 - 2008 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.0.0