0

So I was making a script to check if Firefox is changing my preference on me. When I came across an oddity, see the following script.

check_ff_prefs.sh (this is the bare bones version)

#!/bin/bash

check_match() {
    grep -qx -- "${1}" $2 
    exit_code=$?
    
    if [ "$exit_code" == "0" ]; then
        echo "does match $3"
    else
        echo "does not match $3"
    fi
}

FILE_1=/home/mcm/projects/dotfiles/firefox/prefs.js
FILE_2=/home/mcm/.mozilla/firefox/6zpaipqa.mcm/prefs.js

last_line_file_1=$(tail -n 1 $FILE_1)
last_line_file_2=$(tail -n 1 $FILE_2)

check_match "$last_line_file_1" "$FILE_1" "file1 -> file1"
check_match "$last_line_file_2" "$FILE_2" "file2 -> file2"

check_match "$last_line_file_1" "$FILE_2" "file1 -> file2"
check_match "$last_line_file_2" "$FILE_1" "file2 -> file1"
    
if [ "$last_line_file_1" == "$last_line_file_2" ]; then 
    echo "they match"
else
    echo "they don't match"
fi

echo $last_line_file_1 > last_line_file_1.txt
echo $last_line_file_2 > last_line_file_2.txt

diff last_line_file_1.txt last_line_file_2.txt

The above script's output is below

does match file1 -> file1
does match file2 -> file2
does not match file1 -> file2
does not match file2 -> file1
they do not match
1c1
< user_pref("webgl.vendor-string-override", " ");
---
> user_pref("webgl.vendor-string-override", " ");

As you can see the strings really don't match according to bash, grep and diff.

Note: I have tried escaping the " with this nonsense line=${line//\"/\\\"}

So my question is two fold.

  1. How are these lines actually different
  2. Is there something I can do with grep (or another program) to ignore this difference?

The two files that have output piped to them are here: https://github.com/robbmj/dotfiles/blob/alacritty_mods/firefox/last_line_file_1.txt https://github.com/robbmj/dotfiles/blob/alacritty_mods/firefox/last_line_file_2.txt

Thanks.

robbmj
  • 16,085
  • 8
  • 38
  • 63
  • 1
    You can see in your github that `file_1` is 49 bytes and `file_2` is 48 bytes. So you'll have to view them in a hex editor to see if there is a control character in there some where. – Computable Sep 25 '22 at 02:08
  • You have a bunch of unquoted variable references, which can cause [confusing and misleading results](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) -- I'd fix those first, along with anything else [shellcheck.net](https://www.shellcheck.net) points out. If that doesn't make the problem clear, try piping the output through `cat -vet`, which will convert invisible/weird characters into visible representations, and also add a "$" at the end of each line (so you can see if e.g. there's a space at the end of the line). – Gordon Davisson Sep 25 '22 at 02:45

0 Answers0