Comparison of two file using bash scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparison of two file using bash scripting
# 1  
Old 07-25-2013
Comparison of two file using bash scripting

I need following code in linux bash:


Step 1:


run a command and create file1

run a command and create file2


compare file1 and 2

if any difference go to step 1 (keep trying 4 times, if no success, abort program)

else do nothing and continue program.


now I'm stuck with this comparison and then if loop and iterations. i know diff and comm can be used but unable to understand, How as i dont want any particular output and then how to manage these 4 iterations. can anyone please let me know?
# 2  
Old 07-25-2013
Code:
N=1
while [ $N -le 4 ]
do
        command > file1
        command > file2

        # Break out of the loop if files are identical
        diff file1 file2 >/dev/null && break
        let N=N+1
done

if [ $N -le 4 ]
then
        echo "succeeded"
else
        echo "failed"
fi

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 07-25-2013
Quote:
Originally Posted by kashif.live
I need following code in linux bash:


Step 1:


run a command and create file1

run a command and create file2


compare file1 and 2

if any difference go to step 1 (keep trying 4 times, if no success, abort program)

else do nothing and continue program.


now I'm stuck with this comparison and then if loop and iterations. i know diff and comm can be used but unable to understand, How as i dont want any particular output and then how to manage these 4 iterations. can anyone please let me know?
What are the possible contents of file1 and file2?
Does the comparison need to be performed line by line between file1 and file2, or what kind of comparison do you need? File size?
Why would you need to perform the comparison 4 times? If it fails once, it will fail 4 times. If you're considering to perform the comparison WHILE you are adding contents to any of those files, the execution of the script can fail as you are trying to access a file that is being currently used by another process.
Please provide more details and we'll be more than glad to help.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Save output into file bash scripting

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Hi there. i have created a program that in the end it will give output like this 1 2 3 4 5 10 9 ... (1 Reply)
Discussion started by: shdin271
1 Replies

2. Shell Programming and Scripting

Save output into file bash scripting

Hi there. i have created a program that in the end it will give output like this 1 2 3 4 5 10 9 8 7 6 11 12 13 14 15 .............. 17 i wonder how to save the output into a single string and into a file. i.e 1 10 11 12 9 2 3 8 13 14 7 4 5 6 15 17 (in this order,... (3 Replies)
Discussion started by: shdin271
3 Replies

3. UNIX for Advanced & Expert Users

Comparison in Korn shell scripting

I have a scenario to implement in Korn shell script. Here it is.. I need to compare two values to see whether they are same or not. The issue is that the values coming in for comparison can be a string or an integer which can be determined during run time only. Which korn shell comparison... (2 Replies)
Discussion started by: vani123
2 Replies

4. UNIX for Dummies Questions & Answers

File handling in bash shell scripting

i am new to shell scripting and stuck at one place in my program. i am reading data from one structured file and extracting some data from particular lines and then writing into the output file. In that reading input file line by line from while loop. while read line do rectype=line... (7 Replies)
Discussion started by: reeta_shri
7 Replies

5. Shell Programming and Scripting

Bash Scripting - How to grep a file into an array

I have figured out how to grep the file like this: echo `grep $(date +'%Y-%m-%d') Cos-01.csv | cut -d',' -f1` The above line does echo the correct information from the lines in which my search criteria is found. Now I am trying to get that information (Yes, just one column of every line) into... (6 Replies)
Discussion started by: TwelveDays
6 Replies

6. Shell Programming and Scripting

Comparison of two files which contains strings using Shell scripting or PERL

Hi, I need sample code to compare the two files line by line which contains text strings and to print the difference in the third file. Thanks in advance (1 Reply)
Discussion started by: sudhakaryadav
1 Replies

7. Shell Programming and Scripting

File handling with bash shell scripting

Hi all, Can anyone guide to get tricks for file handling in bash shell? Thanks in advance. Thanks Deepak (2 Replies)
Discussion started by: naw_deepak
2 Replies

8. Shell Programming and Scripting

comparison of strings in unix shell scripting

Hi STORAGE_TYPE=$1 echo "########### SQL SESSION STARTED ###########" VALUE=`sqlplus -S /nolog << THEEND connect tcupro/tcupro_dev@bdd1optn SET SERVEROUTPUT ON DECLARE V_STORAGE_TYPE varchar2(3); V_ERR_MSG varchar2(255) ; V_LOG_LEVEL varchar2(200); BEGIN... (1 Reply)
Discussion started by: piscean_n
1 Replies
Login or Register to Ask a Question