1

I have the following bash script.

while IFS= read -r filename;
  do [[ $(md5 path/to/"$filename-orig") = $(md5 path/to/"$filename") ]] || echo $filename differs;
  done < path/to/list-of-files-to-compare.txt

It's supposed to compare two files (by computing their MD5 hash digest) then report if they are different. It gets the files to compare from a list.

The problem is that if the file I am trying to read is at, say,

path/to/foo-orig.js

the script will look for the file at

path/to/foo.js-orig

and, obviously, this throws an error and fails.

How do I correct this bug in my script so that I handle the .js extension correctly?

Edit

TL;DR:

Given a string foo.bar how can I get foo-orig.bar?

Edit2:

I add the -orig extension because this project takes a template that's regularly updated by a third party, then I overlay my changes to it. So, I want to keep track of what files they change so I can go back and adapt my code when necessary.

list-of-files-to-compare.txt looks like the following.

list-of-files-to-compare.txt
public/assets/images/logos/logo.svg
README.md
src/auth/Auth.js
src/auth/store/actions/login.actions.js
src/auth/store/actions/user.actions.js
src/auth/store/reducers/user.reducer.js
src/firebaseService/firebaseService.js
src/firebaseService/firebaseServiceConfig.js
src/fuse-configs/fuseNavigationConfig.js
src/fuse-configs/fuseRoutesConfig.js
src/index.js
src/main/content/components/ComponentsConfig.js
src/main/content/login/Login.js
src/store.js
src/store/actions/index.js
src/store/reducers/index.js
  • Why do you add -orig in the first place? How does list-of-files-to-compare.txt look like? – Arkadiusz Drabczyk Nov 30 '18 at 7:53
  • @ArkadiuszDrabczyk: Do the edits help answer your questions? – Mowzer Nov 30 '18 at 8:01
1

In Bash you could use Pattern substitution:

while IFS= read -r filename;
do
    echo ${filename/.js/-orig.js}
done < list-of-files-to-compare.txt

If you want to be more generic you can first get an extension with cut and rev like that:

$ ext=$(echo public/assets/images/logos/logo.svg | rev | cut -d. -f1  | rev)
$ echo $ext
svg

And then replace it with sed:

$ echo public/assets/images/logos/logo.svg | sed "s,\.${ext}\$,-orig.${ext},"
public/assets/images/logos/logo-orig.svg

It would be easier if you appended .orig to the filename so that logo.svg would become logo.svg.orig.

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.