Why is the file changing before being written to?
2
votes
2
answers
972
views
On Kubuntu Linux, The Google Chrome browser adds a checksum to the file, preventing simply editing the file by hand. So I'm writing a script to add the checksum.
$ cat .config/google-chrome/Default/Custom\ Dictionary.txt
AATEST
dotancohen
checksum_v1 = 2b7288da7c9556608de620e65308efa4$
No problem, I'll copy the entire file sans last line and check of its MD5 hash matches that checksum.
$ head -n -1 .config/google-chrome/Default/Custom\ Dictionary.txt > ~/chrome-dict
$ cat ~/chrome-dict
AATEST
dotancohen
$ md5sum ~/chrome-dict
2b7288da7c9556608de620e65308efa4 /home/dotancohen/chrome-dict
We got 2b7288da7c9556608de620e65308efa4
, as expected. It matches! So let's add that to the end of the file.
$ { printf "checksum_v1 = " ; printf $(md5sum -z ~/chrome-dict | awk '{print $1}') ; } >> ~/chrome-dict
$ cat ~/chrome-dict
AATEST
dotancohen
checksum_v1 = 08f7dd79a17e12b178a1010057ef5e34$
No, wrong checksum! Lets try cat to ensure that nothing is written to the file between the two printf
statements.
$ head -n -1 .config/google-chrome/Default/Custom\ Dictionary.txt > ~/chrome-dict
$ cat ~/chrome-dict
AATEST
dotancohen
$ { printf "checksum_v1 = " ; printf $(md5sum -z ~/chrome-dict | awk '{print $1}') ; } | cat >> ~/chrome-dict
$ cat ~/chrome-dict
AATEST
dotancohen
checksum_v1 = 08f7dd79a17e12b178a1010057ef5e34$
Still wrong checksum! Let's try a tmp file.
$ head -n -1 .config/google-chrome/Default/Custom\ Dictionary.txt > ~/chrome-dict
$ cat ~/chrome-dict
AATEST
dotancohen
$ { printf "checksum_v1 = " ; printf $(md5sum -z ~/chrome-dict | awk '{print $1}') ; } >> ~/chrome-dict-tmp
$ cat ~/chrome-dict-tmp >> ~/chrome-dict && rm ~/chrome-dict-tmp
$ cat ~/chrome-dict
AATEST
dotancohen
checksum_v1 = 2b7288da7c9556608de620e65308efa4$
That worked! **Why didn't the one-liners that redirect output to the end of the ~/chrome-dict
file return the correct MD5 hash?**
Asked by dotancohen
(16493 rep)
Jan 9, 2025, 09:00 PM
Last activity: Jan 9, 2025, 10:34 PM
Last activity: Jan 9, 2025, 10:34 PM