comparing two arrays or strings in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting comparing two arrays or strings in bash
# 1  
Old 06-16-2008
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:
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

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.

Last edited by Yogesh Sawant; 11-24-2010 at 07:02 AM.. Reason: added code tags
# 2  
Old 06-16-2008
Is this a question about how to handle arrays, or how to find a character in a string? Do you need to know at which index the character matched, or how many matches there are, or just whether or not there was a match? The last is the simplest, so let's start from that.

Code:
#!/bin/sh

echo -n "Enter a string: "
read word

echo -n "Enter a character: "
read char

origword=$word

while true; do
  case $word in
    '') echo "$char not found"; break;;
    ${char}*) echo "$char found in $origword"; break;;
  esac
  word=${word#?}
done

If this is not what you were asking about, perhaps you can clarify which part you still need help with.
# 3  
Old 06-16-2008
comparing two arrays

actually i need to do all what you have mentioned,

- user 1 enter a word
- user2 try to guess by entering a character,
- checking if this character matches any character in the word entered by
player1.
- in case matches any of the characters it displays the character in the
right place.
for example:
player one enter a word:
hello
player two try to guess a word from 5 characters, enter a letter:
-----
player two enters h

output: h----
player 2 enters L :
output : h-ll-

so forth. im having trouble checking comparing what player 2 enters with every single character in the word entered by player 1.
Thanks again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash - Comparing 2 xml strings masking certain fields

Would like to compare 2 XML Strings which has certain known fields changed. For example, Date field will always have differences. When comparing both strings, skip/mask all the occurring Date Field's `DtField1` and `DtField2` Note: these are not formatted xml format. File1: ... (1 Reply)
Discussion started by: Sajjadmehdi
1 Replies

2. Shell Programming and Scripting

awk arrays comparing multiple columns across two files.

Hi, I'm trying to use awk arrays to compare values across two files based on multiple columns. I've attempted to load file 2 into an array and compare with values in file 1, but success has been absent. If anyone has any suggestions (and I'm not even sure if my script so far is on the right lines)... (4 Replies)
Discussion started by: hubleo
4 Replies

3. Shell Programming and Scripting

Comparing 2 arrays but ignoring certain patterns

I'm trying to compare 2 array and print the difference at a 3rd file. However how am i going to compare this 2 arrays by ignoring certain patterns: For example: 1st array contains: ctahci cptcsa0 ctsata:25:seed cptcsa1:50:seed ctsata_1:25:seed 2nd array contains: cptcsa0 ctsata... (0 Replies)
Discussion started by: hongping
0 Replies

4. Shell Programming and Scripting

Using arrays in bash using strings to bash built-in true

I have the following code and for some reason when I call the program using /home/tcdata/tatsh/trunk/hstmy/bin/bash/raytrac.bash --cmod=jcdint.cmod I get hasArgument = hasArgument = true Somehow the array element is returning even though I have not chosen the option. ... (41 Replies)
Discussion started by: kristinu
41 Replies

5. UNIX for Dummies Questions & Answers

Comparing lists.. Arrays maybe?

Problem Part 1. Gather data from linux server and output to a file named data_DDMMYY Output file must contain the file name and size Part 2. Compare todays data_DDMMYY to yesterdays data_DDMMYY and output results to a file named difference_DDMMYY Output file must show the difference in... (3 Replies)
Discussion started by: Aussiemick
3 Replies

6. Shell Programming and Scripting

How to make arrays from strings in bash?

Hey all, This is my first post, and I am relatively new to linux/unix scripts. I am writing a bash script in which I am trying to extract one line from another file and parse specific words from the line into an array. So for example, I have a file called SortScans in which the first 5 lines... (9 Replies)
Discussion started by: camocazi
9 Replies

7. UNIX for Dummies Questions & Answers

Strings in arrays

Hi There, Code: Number=10 i=1 while do echo "$i" Check=`find $viewing -name $File_Pattern -type f -Print` i=`expr ${i} + 1` done Unable to store the values in variable 'Check', when i display i am getting Check nothing... (1 Reply)
Discussion started by: Naveen_5960
1 Replies

8. Shell Programming and Scripting

PERL: simple comparing arrays question

Hi there, i have been trying different methods and i wonder if somebody could explain to me how i would perform a comparison on two arrays for example my @array1 = ("gary" ,"peter", "paul"); my @array2 = ("gary" ,"peter", "joe"); I have two arrays above, and i want to something like this... (5 Replies)
Discussion started by: hcclnoodles
5 Replies

9. Shell Programming and Scripting

Comparing arrays in perl

Hi all, I am trying to compare two arrays in perl using the following code. foreach $item (@arrayA){ push(@arrayC, $item) unless grep(/$item/, @arrayB); ... (1 Reply)
Discussion started by: chriss_58
1 Replies

10. Shell Programming and Scripting

Comparing Arrays?

Is there anyway that I can compare two Arrays to see if any new strings have been added in them? eg: Array 1: Joe Bob Jane Array 2: Joe Bob Jane Greg It would then output a new array with the changes: Array 3: Greg I'm not very good at shell scripting, and my google and forum searches... (4 Replies)
Discussion started by: blckleprd
4 Replies
Login or Register to Ask a Question