Compare file sizes in shell script

I am trying to compare the size of two files in a shell script, but I get an error : 32: 8: unexpected statement .

I=`wc -c $i | cut -d' ' -f1` J=`wc -c $j | cut -d' ' -f1` if test $I == $J then echo $i $j >> $1.pares fi 

I am testing the values ​​in $ I and $ J using an echo, and the values ​​are correct, but I cannot compare them ...

+4
source share
4 answers

Try using square brackets ( [] ) and -eq as follows:

 I=`wc -c $i | cut -d' ' -f1` J=`wc -c $j | cut -d' ' -f1` if [ $I -eq $J ] then   echo $i $j >> $1.pares fi 
0
source

it works on bash

 if((`stat -c%s "$file1"`==`stat -c%s "$file2"`));then echo "do something" fi 
+4
source

Try

 I=`wc -c "$i"` # always use quoted var J=`wc -c "$j"` [[ "$I" == "$J" ]] && echo "$i" "$j" >> "$1".pares 

Always specify variables, because you can have a file name containing a space.

Despite the fact that BASH is case insensitive with variable names, it is better and safer to use a different (and longer than one char) name for the variables.

0
source

Algo assim funcionaria ....

 #/bin/bash <br> I=`wc -c < echo $i` J=`wc -c < echo $j` if [ $I -eq $J ]; then echo $i $j >> $1.pares fi 

AbraΓ§os!

0
source

Source: https://habr.com/ru/post/1380955/


All Articles