0

Need some ideas here. I was using something along the lines of if $[$x-$y>1500] then etc.... to detect the size of the changes to a file. I've realized that won't work as the files sizes could increase or decrease which could result in a negative number. Is there a way to use absolute value of x-y?

2
  • remove the hyphen, it'll then be taken as positive
    – gwillie
    May 10, 2017 at 1:40
  • Do any of the provided answers solve your problem? If so, don't forget to mark one with the checkmark; thank you!
    – Jeff Schaller
    May 21, 2017 at 11:25

2 Answers 2

0

I have done this. I create a new file (with at ".new" suffix) but before replacing the old I would check the size difference of the files, and abort (sending some type of notification, such as mail) if there are too many changes.

I generally do this in perl, but bash would be similar.

$file="file_being_updated";
$new=".new";

if ( -f $file ) {
  my $percent_diff = abs( 100 - 100*(-s "$file$new")/(-s $file) );
  if (  $percent_diff > 20 ) {   # more that this to different!
    printf STDERR "File \"$file$new\" differs by more that 20%%! (%.1f%%)\n",  $percent_diff;
    printf STDERR "-------------- ABORTING REPLACMENT -----------\n";
    exit 10;
  }
}
0
diff=$((x - y)
diff=$(( (diff > 0) ? $diff : -1*$diff ))
if [ $diff -gt 1500 ]
then
  echo do something
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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