I would like to compare 2 files using diff and then if it returns nothing then echo "no diff" else display the difference. My current if statement only works if there is no difference but if there is then it gives me a "not found" error.
This is what the test.sh looks like:
#!/bin/zsh
if [$(diff file1 file2) = '']
then
echo "no diff"
else
diff file1 file2
fi
If files are same:
$ sh test.sh
no diff
If files are different:
$ cat file1
hello
$ cat file2
goodbye
$ sh test.sh
test.sh: 1: test.sh: [1,8c1,8: not found
< hello
---
> goodbye
In my search to find solutions, I found that having spaces if [ $(diff vs if [$(diff made a difference and using a single = vs == also helped. I figured the problem must be that I'm testing the output for '' (nothing). It actually does work but the "not found" line doesn't belong there. I don't just want to silence that annoying line but do the if statement the proper way because there should be a way to get the result I need without having to run the diff twice.
1,8c1,8as part of the output. How can I get rid of that? EDIT: Wait, I see that's fromdiffitself and the-soption is actually what I needed. Thanks. – ntruter42 May 24 '20 at 03:13