need help to compare 2 files inside a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need help to compare 2 files inside a loop
# 1  
Old 11-20-2011
Question need help to compare 2 files inside a loop

Basically, I have a user input a colour into a variable, and then i echo that variable into a text file. Then I need to compare those 2 files which is easy using the diff command. The thing I can not get is how to do this inside of a loop until the variable matches the file. Also if the user enters an incorrect colour, the script needs to prompt to re-enter until the colour is correct. Case sensitive is not an issue.

EXAMPLE
Code:
cat color_names.txt

Blue
Yellow
Red



Code:
#prompt user to enter colour
read -p "Please enter a text colour: " TextColour


#Put variable into a text file
echo $TextColour > ./TextColour.txt


#Loop until the colour is correct

Here I am not sure what kind of loop to use, and I have no idea what the syntax would be. Any help would be greatly appreciated

Thanks

Last edited by Franklin52; 11-21-2011 at 08:06 AM.. Reason: Code tags
# 2  
Old 11-20-2011
This might help get you started:

Code:
while read -p "enter colour: "  colour 
do 
    echo $colour; 
done

# 3  
Old 11-20-2011
Thanks but that isnt what the problem is. I dont know how to loop it until the user enters a colour that is inside the color_names.txt file
# 4  
Old 11-20-2011
If /tmp/colour.txt contains the list of colours that are acceptable:

Code:
while read -p "enter colour: "  colour 
do 
    if grep -q ${colour:-foo} /tmp/colour.txt
    then
       echo right
       break          # exit the loop when a colour is in the file. 
    fi
done

# 5  
Old 11-20-2011
This is good.....although I think it would be easier using an until loop. You will have to edit this because i know its not going to be right but for example....
Code:
read -p "colour : " colour

until grep -i "$colour" "colour_names.txt"
do *something*
done


My apologies for the loop...I am just learning. I tried your while loop but it doesnt seem to want to work.....even if the colour is right, it just keeps prompting to re renter


Moderator's Comments:
Mod Comment Please use code tags when posting data and code samples, thank you.

Last edited by Franklin52; 11-21-2011 at 08:07 AM..
# 6  
Old 11-20-2011
No need to apologise -- we all started at some point.

As far as the loop not working, is it echoing "right" when the proper colour name is entered and just not breaking? I've run it under bash and it does fine for me.

As for the until loop -- what ever works for you.
# 7  
Old 11-21-2011
Thanks

Thanks for all your help. I have figured out the solution
Code:
until   echo "$TextColour" | grep -iq "^"$TextColour"$" color_names.txt
 do
  read -p "please re-try: " TextColour
done


Last edited by Franklin52; 11-22-2011 at 04:00 AM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Write a while loop inside for loop?

I'm taking a unix class and need to countdown to 0 from whatever number the user inputs. I know how to do this with a while or until loop but using the for loop is throwing me off.... I know I can use an if-then statement in my for loop but can I include a while loop in my for loop? (3 Replies)
Discussion started by: xxhieixx
3 Replies

2. Shell Programming and Scripting

Issue with copying files into dir inside for loop

Hi , I'm trying to move/copy the files inside the loop into a directory . I tried the below code and the issue is the data is not copying into the created directory but the files are copying into another file file_path="/home/etc" Last_Day=20130930 mkdir $file_path/ARC_${Last_Day} ... (3 Replies)
Discussion started by: smile689
3 Replies

3. Shell Programming and Scripting

Avoiding some files inside a loop

In my script I need to loop around some files like below example files are fa.info.abcd fa.info.bxde fa.info.cdas ------ for test_data in fa.info.* do # Some text appending logic applied # Copy to another directory done Now I need to discard some files while looping around ... (9 Replies)
Discussion started by: smile689
9 Replies

4. Shell Programming and Scripting

For loop inside awk to read and print contents of files

Hello, I have a set of files Xfile0001 - Xfile0021, and the content of this files (one at a time) needs to be printed between some line (lines start with word "Generated") that I am extracting from another file called file7.txt and all the output goes into output.txt. First I tried creating a for... (5 Replies)
Discussion started by: jaldo0805
5 Replies

5. Shell Programming and Scripting

Renumber position 88-94 inside all files matching criteria inside folder

There are 4 files inside one folder matching criteria i.e. File name = ABCJmdmfbsjopXXXXXXX_mm-dd-yyyy_XXX.data Here is the Code which find the files matching criteria:- TS=`date +"%m-%d-%Y"`| for fname in `find . -name "ABCJmdmfbsjop???????_${TS}*.data"` do # Matching File Processing Code.... (1 Reply)
Discussion started by: lancesunny
1 Replies

6. Shell Programming and Scripting

[Solved] Endless while loop when compare files

Hi All, I've written a script to read 2 files and compare the contents using while loop but somehow when $line is not found in test2, the script will continue looping. Below is my code, pls advise what could went wrong TIA Nick for line in test1.txt | while read line do grep -i... (4 Replies)
Discussion started by: Nick1971
4 Replies

7. Shell Programming and Scripting

compare date and time inside data of two files

i have two files with identical no of columns. 6th columns is date (MM/DD/YY format) and 7th columns is time (HH:MM:SS) format. I need to compare these two vaules and if the date & time is higher than fileA, save it on fileC; if the value is lower, then save it on fileD CONDITIONS... (7 Replies)
Discussion started by: ajiwww
7 Replies

8. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

9. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

10. Shell Programming and Scripting

for loop, calling and editing multiple files inside

hey guys, I'm trying to call and modify multiple files inside the for loop, i can't get it to work... ------------------------ AFILE=/dir/a_file.txt BFILE=/dir/b_file.txt CFILE=/dir/c_file.txt ADESTFILE=/dir/a_dest_file.txt BDESTFILE=/dir/b_dest_file.txt... (6 Replies)
Discussion started by: DeuceLee
6 Replies
Login or Register to Ask a Question