Bash中的Diff命令

Dio*_*rou 0 bash diff

每次我运行以下bash命令时,都会出现错误:

这是代码:

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
Run Code Online (Sandbox Code Playgroud)

这是错误:

./test.sh: 2c2: not found
Run Code Online (Sandbox Code Playgroud)

我基本上想对两个文件进行排序,然后检查它们是否相等。我不明白的是此错误的含义以及如何消除它。任何帮助将不胜感激。

谢谢!

pas*_*qui 5

简短答案:使用

diff $studentFile $profFile
Run Code Online (Sandbox Code Playgroud)

代替:

$(diff $studentFile $profFile)
Run Code Online (Sandbox Code Playgroud)

长答案:

diff $studentFile $profFile 
Run Code Online (Sandbox Code Playgroud)

将提供几行的输出,在您的示例中,第一行是“ 2c2”。如果将diff命令包含在$()中,则此表达式的结果为“ 2c2 ...”。现在,bash将此结果用作新命令,结果为“找不到命令:2c2”。

比较示例:

$(diff $studentFile $profFile)
Run Code Online (Sandbox Code Playgroud)

和:

echo $(diff $studentFile $profFile)
Run Code Online (Sandbox Code Playgroud)

*附录*

if diff $studentFile $profFile > /dev/null 2>&1
then
  echo "equal files"
else
  echo "different files"
fi
Run Code Online (Sandbox Code Playgroud)