1

Every time i run the following bash command, i get an error:

Here's the code:

sort -b ./tests/out/$scenario > $studentFile
sort -b ./tests/out/$scenario > $profFile
$(diff $studentFile $profFile)
  if [ $? -eq 0 ]
      then 
          echo "Files are equal!"
      else
          echo "Files are different!"
      fi

Here's the error:

./test.sh: 2c2: not found

I basically want to sort two files, and then check if they are equal or not. What i don't understand is what this error means and how i can get rid of it. Any help will be greatly appreciated.

Thanks!

2
  • Any spaces, stars, or other special characters in the filenames or paths? Commented May 31, 2015 at 15:48
  • no, just regular text @user000001 Commented May 31, 2015 at 15:49

2 Answers 2

7

Short answer: use

diff $studentFile $profFile

instead of:

$(diff $studentFile $profFile)

Long answer:

diff $studentFile $profFile 

will provide an output of several lines, first one, in your example, is "2c2". If you enclose the diff command in $(), the result of this expression is an string with the concatenation of all the lines, "2c2 ...". In your script, this result is executed by bash as new command, with the result of "command not found: 2c2".

Compare, by example:

$(diff $studentFile $profFile)

and:

echo $(diff $studentFile $profFile)

*** Addendum ***

if diff $studentFile $profFile > /dev/null 2>&1
then
  echo "equal files"
else
  echo "different files"
fi

is a possible way of reach the expected result.

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry i forgot the last part of the code i've edited it!
Same problem there: remove all $( ... ) around the diff commands.
Thanks, turned out it was an easy fix!
2

Your command

$(diff $studentFile $profFile)

executes the result of running the diff command on the two files. The 2c2 that your shell complains about is probably the first word in the output from diff.

I'm assuming that you simply want to see the output from diff:

diff $studentFile $profFile

If you want to compare the file for equality in a script, consider using cmp:

if cmp -s $studentFile $profFile; then
    # Handle file that are equal
else
    # Handle files that are different
fi

The diff utility is for inspecting differences between files, while cmp is much better suited for just simply testing if files are different (in a script, for example).

For more specialized cases there is the comm utility that compares lines in sorted files.

3 Comments

Sorry i forgot the last part of the code i've edited it now !
Uh, why? That's a strange requirement.
cmp is much more efficient than diff. I would say use diff when you want to see the difference in a nicely formatted way, but use cmp if you just want to compare for equality.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.