I am learning linux and was given this problem as my homework but i can't solve this that how we can compare two files content in shell mode. (Here, we can assume that both files having text content like this for example: cat > f1 this is file 1)

$ cat duplicate_file.sh
echo "Enter file 1:"
read file1
echo "Enter file 2:"
read file2
cmp $file1 $file2 > newfile
x=` wc newfile | cut -d" " -f2 `
if [` $x -eq 0 `]
rm -i $file2

I made this program but this isn't working. Any suggestions?

closed as off-topic by Christian Brabandt, Ingo Karkat, grodzik, D. Ben Knoble, Vitor Nov 8 at 23:50

  • This question does not appear to be about Vi and Vim family of editors, within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    this is the wrong stackexchange site. It should probably be moved to unix.stackexchange.com – Christian Brabandt Nov 8 at 6:44
  • 5
    I'm voting to close this question as off-topic because it has nothing to do with vi. – Christian Brabandt Nov 8 at 7:28
  • @Christian I didn't knew that there is another site like unix.stackexchange.com where i could have asked this question. And there is no reason of putting this on hold as i was just asking how we can compare two scripts? This is definitely related to vi -_- – Hariom kushwaha Nov 10 at 5:57
  • Why is this related to vi? It is a shell script question and has nothing to do with Vi. – Christian Brabandt Nov 10 at 18:16

The cmp command returns a short summary of the changes (e.g. file1 file2 differ: byte 3, line 1), but that's purely for the user (and what you're trying to do with the wc and cut parsing is beyond me). In a script, you should rather use the exit status of the command, and ignore (or even suppress) the output altogether. From man cmp:

Exit status is 0 if inputs are the same, 1 if different, 2 if trouble.

Therefore, something like this would work:

if cmp $file1 $file2 >/dev/null
    rm -i $file2

You'll often also see the following, shorter syntax:

cmp $file1 $file2 >/dev/null && rm -i $file2

Additional critique

  • Command substitution via `...` is old syntax; better use $(...) instead.
  • If your files contain special characters (e.g. whitespace or *), you'll run into problems. Robust shell scripts should properly quote all variables! Use cmp "$file1" "$file2", and rm -i "$file2" instead.
  • Actually, i was trying to count no. of words in both files by using 'wc' (word count) and 'cut' along with delimiter (-d) was used here to count the spaces " ", so that given equation will assign a constant value to x. And then i tried equating it with 0 and then removing either one of the file if their wc comes equal. – Hariom kushwaha Nov 10 at 6:05
  • I know, it is wrong (me using wc and cut) but i didnt knew wt to do with this that's why i did so. (actually, i tried applying a method i learned while using python and this is the reason i tried making this algorithm) :( – Hariom kushwaha Nov 10 at 6:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.