Fedora Linux Support Community & Resources Center
  #1  
Old 16th June 2008, 06:43 PM
new2unix Offline
Registered User
 
Join Date: Dec 2007
Posts: 52
comparing two arrays or strings in bash

Hi there,
im having issue with comparing two variables, in a bash script.
im trying to do the following:
- get a word from user 1
- split the word into array
- get a character from user2

trying to compare the character entered by user 2 with every single character in the array entered by user1. here is the code:
#######################################
echo "$name1 Enter a word: "
read word
wordarray=(`echo $word | sed 's/./& /g'`) # i used sed to convert it to
# array
echo "$name2 enter a character: "
read guess
guessarray'('echo $guess | sed 's/./&/g'`) # convert the user2 entry to character
#####################################
and then from here, im having trouble finding a way to compare the entry of user2 with every single item in the array entered by user1.

Thanks in advance.
Reply With Quote
  #2  
Old 16th June 2008, 06:56 PM
new2unix Offline
Registered User
 
Join Date: Dec 2007
Posts: 52
how to avoid entering wildcards in a word

Hi everybody,
im writing a bash script, and receiving entry from a user, and i need to check if the word has any wildcards in.
do u have any ideas please .

thanks .
Reply With Quote
  #3  
Old 16th June 2008, 07:07 PM
nick.stumpos's Avatar
nick.stumpos Offline
Registered User
 
Join Date: Feb 2005
Location: Lansing, Mi
Age: 28
Posts: 2,222
use a backslash before the wildcard you are searching for to escape it
__________________
As always
Love, Life, Loyalty, Wisdom, Knowledge, And Understanding
FC6: Common Questions answered
Reply With Quote
  #4  
Old 16th June 2008, 07:09 PM
nick.stumpos's Avatar
nick.stumpos Offline
Registered User
 
Join Date: Feb 2005
Location: Lansing, Mi
Age: 28
Posts: 2,222
http://www.google.com/search?q=bash+scripting
sorry if that seems rude, but this is the second simple bash scripting question you have in a row
__________________
As always
Love, Life, Loyalty, Wisdom, Knowledge, And Understanding
FC6: Common Questions answered
Reply With Quote
  #5  
Old 16th June 2008, 07:13 PM
leigh123linux's Avatar
leigh123linux Offline
Retired Administrator
 
Join Date: Oct 2006
Posts: 21,509
Threads merged ! , Please stick to one thread next time .
__________________
My Hardware
- CPU: AMD Phenom II X6 Hex Core 1055T 95W Edition @3.5Ghz
- Motherboard: Gigabyte GA-880GM-UD2H
- Cooler: Corsair H50 CPU Cooler
- RAM: Corsair Dominator 8GB (4x2GB) DDR3 1600MHz
- Graphics: Gigabyte GeForce GTS 450 OC 1024MB GDDR5
Reply With Quote
  #6  
Old 16th June 2008, 07:27 PM
new2unix Offline
Registered User
 
Join Date: Dec 2007
Posts: 52
Sorry, for posting more than one at a time.

Sorry i might didnt explain my need well,

echo " enter a word"
read word

i need to write if statement to check if word entered has any wildcards in
and i dont khow how to represent it.

i know how to convert the word entered to array, by the following
wordarray=(`echo $word | sed 's/./& /g'`)
and then i need to check it
if worddarray has any wildcards in .

thanks again.
Reply With Quote
  #7  
Old 16th June 2008, 08:15 PM
Vouters Offline
Banned
 
Join Date: Oct 2005
Posts: 190
Without an array solution::

#!/bin/bash -x
#
typeset j integer
found=0;
echo "Enter a word: "
read word
echo "Enter a character: "
read guess
for i in `echo $word | sed 's/./& /g'`; do
if [ $guess == $i ];
then
found=1
break
fi
done
if [ $found == 0 ];
then
echo "$guess not in $word"
else
echo "$guess in $word"
fi
exit 0

With an array solution:

#!/bin/bash -x
#
typeset j integer
found=0;
echo "Enter a word: "
read word
echo "Enter a character: "
read guess
wordarray=(`echo $word | sed 's/./& /g'`)
j=0
until [ $j == ${#wordarray[@]} ];
do
if [ $guess = ${wordarray[$j]} ];
then
found=1
break
fi
let j+=1
done
if [ $found == 0 ];
then
echo "$guess not in ${wordarray[@]}"
else
echo "$guess in ${wordarray[@]}"
fi
exit 0
Reply With Quote
  #8  
Old 16th June 2008, 10:26 PM
new2unix Offline
Registered User
 
Join Date: Dec 2007
Posts: 52
Thanks a lot, ive had some errors, but i was able to fix them.
Thanks, and sorry for posting two thread in row, i wasnt aware of this.
Reply With Quote
Reply

Tags
arrays, bash, comparing, strings

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
comparing passwords dlox Security and Privacy 5 8th May 2006 09:57 PM
PHP arrays... fnord Programming & Packaging 12 5th December 2005 09:59 PM
Arrays in C eggsy85 Programming & Packaging 14 23rd April 2005 05:36 AM
Semaphore Arrays LackeyLad Programming & Packaging 2 5th November 2004 05:51 AM


Current GMT-time: 03:21 (Monday, 20-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat